Creating an array of key-value pairs(fetched from a mongoDB database) dynamically

59 views
Skip to first unread message

Aryak Sengupta

unread,
Mar 14, 2015, 9:17:28 AM3/14/15
to nod...@googlegroups.com
I am trying to create an array of Key-Value pairs dynamically with a document from a MongoDB database .

I have posted my question on Stack Overflow but still I can't figure out where I am going wrong. This is my question ->
 
Creating an array of key-value pairs(fetched from a mongoDB database) dynamically

Any help will be appreciated, 
Thank You. 

Angel Java Lopez

unread,
Mar 14, 2015, 9:42:56 AM3/14/15
to nod...@googlegroups.com
Hi!

I didn't run this code, but maybe is a solution

router.get('/account',isAuthenticated, function(req,res){
   var following_users_details = [];
   var i = 0;
   
   getOneUser();
   
   function getOneUser() {
   
        var following_user = req.user.following_users[i++];
        
        if (!following_user) {
            res.render('users/userAccount',{user:req.user, following_users_details:following_users_details });
            return;
        }
        
        User.findOne({'username' : following_user },
            function(err,user)
            {
                if (!err) 
                    following_users_details.push({
                        username: user.username,
                        profilepic : user.photo,
                        userbio : user.bio
                    });
                    
                setTimeout(getOneUser, 0);
            });
    }
});

The original code returned a result BEFORE the find one user executes the callback.

The above code retrieves one user, and then, in the callback, invoke the same function to retrieve another user. It should be better solutions, but I hope this code works.

Understanding callbacks/async is the price to pay to live at Node.js paradise ;-)

Angel "Java" Lopez
@ajlopez


--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/CALTbq1w%2B5Wz-RWmtSWADc%3Dn%2BF9tP4aYOqs-_S%3D7jbeBaawerKg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Aryak Sengupta

unread,
Mar 14, 2015, 10:15:26 AM3/14/15
to nod...@googlegroups.com
Hi Angel,  

Thanks for your reply.I resolved it. What you mentioned in the end is absolutely true. 


"Understanding callbacks/async is the price to pay to live at Node.js paradise ;-"

I want to dive in deeper to async callbacks. Can you refer me some resources which will help me to grasp the concept properly. 

Thanks again. 



zladuric

unread,
Mar 15, 2015, 10:44:13 AM3/15/15
to nod...@googlegroups.com
Your query is also not optimal.

As I wrote on your SO question, you should do something like this:

    User.find({
        // setup query
        username: {$in: req.user.followed_users}
    },
    {
        // setup fields to return
        username: true,
        profilepic: true,
        userbio: true
      },
      // callback
      function (err, followedUsers) {
        res.json(followedUsers); // or render or whatever.
      });
  },

Aryak Sengupta

unread,
Mar 15, 2015, 11:07:56 AM3/15/15
to nod...@googlegroups.com
That is exactly what I did to resolve it. Thanks for your reply. 

Reply all
Reply to author
Forward
0 new messages