javascript - NodeJS MongoDB adapter interprets string as literal instead of value -


i have following code:

var keyexp = 'experiencecat' + req.param('category'); user.native(function(err,collection) {     collection.find({'_id':{$in:usersarray}},{fields:{'name':1,'avatar':1,'fbid':1,'country':1,keyexp:1}}).toarray(function (err,userprofiles)     {         res.send(userprofiles);     }); }); 

the problem instead of changing keyexp current value (which should "experiencecat0" example) it's trying find column name "keyexp" of course doesn't exist.

how should code able read correct column based on req.param('category') parameter?

here's how should today:

var fields = {'name':1,'avatar':1,'fbid':1,'country':1}; fields['experiencecat' + req.param('category')] = 1; user.native(function(err,collection){     collection.find({'_id':{$in:usersarray}},{fields:fields}).toarray(function (err,userprofiles)     {         res.send(userprofiles);     }); }); 

with es6 you'll able express in more concise way:

var keyexp = 'experiencecat' + req.param('category'); user.native(function(err,collection){     collection.find( ... ,'fbid':1,'country':1,[keyexp]:1}}).toarray(function (err,userprofiles)                                                ^^^^^^^^     {         res.send(userprofiles);     }); }); 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -