locked
Passing request as a parameter to a function RRS feed

  • Question

  • In my custom Azure API, I want to pass request as a parameter to a function. From that function I want to be able to access query parameters and also use service.mssql to execute a query on my database. However request.query.fieldName is coming up null within the function.

        exports.post = function(request, response) {
        console.log("passed UserID = " + request.query.UserID);
        request.service.mssql.query(query, {
            success: function(results) {
            if (foo) {
                callFunction(results[0], request);
            }
        });
        
        function callFunction(firstResult, request) {
            console.log("UserID received in function = " + request.query.UserID);
        }

    In my console I see:

        Information    passed UserID = 120
    
        Error    Error in script '/api/testapi.js'. TypeError: Cannot read property 'UserID' of undefined

    Is there something special about request that means it can't be passed to a new function?

    Thursday, March 5, 2015 10:40 PM

Answers

  • d love to see some documentation to this effect, but it appears that the variables request and response must be the first two passed to the function if they're to work correctly. This code will work:
    exports.post = function(request, response) {
    console.log("passed UserID = " + request.query.UserID);
    request.service.mssql.query(query, {
        success: function(results) {
        if (foo) {
            callFunction(request, response, results[0]);
        }
    });
    
    function callFunction(request, response, firstResult) {
        console.log("UserID received in function = " + request.query.UserID);
    }
    • Marked as answer by Scott_GA Friday, March 6, 2015 2:45 AM
    Friday, March 6, 2015 2:45 AM