简体   繁体   中英

Variable not retaining value outside of function

I am attempting to pass the data variable from the function to the app.get call. But the global variable 'data' in my function is not retaining the value returned from the database query. I believe this to be a scoping issue. I set the value for 'data' to null at the beginning to make the variable accessible throughout the function. I am not hitting either of the error conditions.

function getCommands(query) {
    var data = null;
    try {

    pg.connect(cString, function(err, client, done) {
        // Catch any connection errors, and display them to the screen
        if(err) {
            return console.error('could not connect to postgres', err);
        }

        client.query(query, function(q_err, result) {

        // Release the client back to the pool
        done();

        if(q_err) {
            return console.error('error running query', q_err);
        }

        // Data object with an empty array to hold the row information
        data = {"data":[]};

        for (var row in result.rows)
        {
            //console.log(result.rows[row]);
            data.data.push(result.rows[row]);
        }
        //Here, data, has the correct return values.
        console.log(data);
        });

    });

    }
    catch( e )
    {
        console.log(e);
    }
    //Here, data, is null.
    console.log(data);
    return data;

}

app.get('/clients/', function(req, res) {
    res.send(getCommands('SELECT clientid, clientname FROM hourglass.clients ORDER BY clientid ASC'));
});

Could someone help me determine why 'data' is not retaining value outside of the pg.connect function?

I think your problem is not that the variable is not retaining the value outside of the function, but rather that you console.log(data) is executing before the variable is set.

If you were to put console.log('step X') in your code as in the following example you'll see the sequence in which your code is executed.

function getCommands(query) {
    var data = null;
    console.log('STEP 1');

    pg.connect(cString, function(err, client, done) {

        console.log('STEP 3');

    });

    console.log('STEP 2');

}
function getCommands(query, callback) {

try {

    pg.connect(cString, function(err, client, done) {
        // Catch any connection errors, and display them to the screen
        if(err) {
            return console.error('could not connect to postgres', err);
        }

        client.query(query, function(q_err, result) {

            // Release the client back to the pool
            done();

            if(q_err) {
                return console.error('error running query', q_err);
            }

            // Data object with an empty array to hold the row information
            var data = {"data":[]};

            for (var row in result.rows)
            {
                data.data.push(result.rows[row]);
            }
            callback(data); //After data is set, the value is passed back
        });
    });
}
catch( e )
{
    console.log(e);
}
}

app.get('/clients', function(req, res) {.....])

Using a callback function mentioned by @dystroy worked perfectly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM