0

I am using node.js and mongoose to carry out several HTTP requests including a get request. My get function will have quite a bit of functionality and to ease handling of many of the data, I am trying to have local variables store the returns from mongo. For example:

router.get('/getstuff/:test', function(req, res) {
    var testId = req.params.test;
    var returnStuff = null;

    var collection = req.collection;

    collection.find({userIdd : testId}, function(err, data){
        if (err) console.log(err);
        else {
            console.log(data); // works, data is shown in log
            returnStuff = data; // does not work, data is not saved to returnStuff
        }
    });

    console.log(returnStuff); // undefined
    res.send();
});

I am trying to get what I get back from the database, an array, to the returnStuff variable, but due to the closure, I am unable to do so. This may seem trivial, but as I said, I will have a lot more operations and this would really simplify things.

Anyone have any tips? Really appreciate it.

Thanks

intl
  • 2,753
  • 9
  • 45
  • 71
  • Also note "[How to avoid long nesting of asynchronous functions in Node.js](http://stackoverflow.com/questions/4234619/how-to-avoid-long-nesting-of-asynchronous-functions-in-node-js)" – Jonathan Lonowski Oct 10 '14 at 02:22

1 Answers1

1

collection.find is executed asynchronously, so res.send is executed before returnStuff is filled. you could get rid of it altogether and just res.send(data) in the callback.

That should work :

router.get('/getstuff/:test', function(req, res) {
    var testId = req.params.test;
    var returnStuff = null; //optional, remove if you don't need it for anything else

    var collection = req.collection;

    collection.find({userIdd : testId}, function(err, data){
        if (err) console.log(err);
        else {
            console.log(data); // works, data is shown in log
            returnStuff = data;
            collection.somethingelse(function(err,data2){
                returnStuff += data2
                res.send(returnStuff);
            });                
        }
    });

});

if you have many of these operations, you can make them into a library, or use libraries like async Check out this great resource for more info : http://book.mixu.net/node/ch7.html

Read this post too, and you'll know EVERYTHING!

Community
  • 1
  • 1
xShirase
  • 11,975
  • 4
  • 53
  • 85
  • The thing is, I will have other mongoose db calls in that get function and I will get additional data from those. For that reason, I need to access returnStuff afterwards and use the other data I will be getting as well. Also, as far as I understand (although not much, I'm new to node.js), I can only use res.send() once. I would like to return an overall result afterwards, nothing like returnStuff that's just data for computation. – intl Oct 10 '14 at 02:10
  • in that case, you'll have to nest callbacks, or use a library like async to execute your operations synchronously : have a look at the link I provide in my answer – xShirase Oct 10 '14 at 02:13
  • I've resorted to nested callbacks. Coming from Python and C++, node.js is... different. – intl Oct 10 '14 at 05:40
  • It is, but once you get the hang of it, it's extremely pleasant to work with! – xShirase Oct 10 '14 at 05:57