0

How do I set a variable to a query? I am trying to use functions and callbacks in node.js to work through async, but I am not sure how to get a query to equal to a variable. What I am trying to do in this code is take a friend collection that belongs to a user and return the friends result(which I don't think I am doing correctly in the query insertAll) and then find the user's info for each of the query. And then return the results as a render. I am not sure how to call render either with this...

Here is my code:

exports.contactList = function(req, res) {

    var insertFriend = function(data, callback) {
        var friend = User.findById({_id: user.friendStatus.fuId}, function() {
            callback(null, data);
        }, friend);
    };;

    var insertAll = function(coll, callback) {
        var queue = coll.slice(0),
        friendX;

        (function iterate(){
            if(queue.length === 0) {
                callback();
                return;
            }
            friendX = queue.splice(0,1)[0];
            insertFriend(friendX, function(err, friendX) {
                if(err) {throw err;}
                console.log(friendX + ' inserted');
                process.nextTick(iterate);

            });
        })();
    };



    insertAll([Friend.findOne({userId: req.signedCookies.userid})], function(){

    });

};

1 Answer 1

2

A Query object is returned if you do not pass a callback.

From http://mongoosejs.com/docs/queries.html:

When a callback function:

is passed, the operation will be executed immediately with the results passed to the callback.

is not passed, an instance of Query is returned, which provides a special QueryBuilder interface for you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.