Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

javascript - How to set mongo field from variable

for(var key in resp.updated_items){
    Meteor.users.update({backend_id: Meteor.user().backend_id},
    {$set: {'profile': {key: resp.updated_items[key]}}});
}

And this query set proper information to key key. But how can I assign 'real' value of key?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Basics:

var obj = {key: 'value'}; //prints {key: "value"}
var obj2 = {};
var key = 'someKey';
obj2[key] = 'someValue';
//{someKey: 'someValue'}

Try this: (You have to use [] for that.)

for(var key in resp.updated_items){
  var set = {profile: {}};
  set.profile[key] = resp.updated_items[key];
  Meteor.users.update({backend_id: Meteor.user().backend_id},
  {$set: set});
}

I am not able to remeber how set works in meteor. But in mongo, you have to do something like this:

for(var key in resp.updated_items){
  var set = {};
  set['profile.' + key] = resp.updated_items[key];
  Meteor.users.update({backend_id: Meteor.user().backend_id},
  {$set: set});
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...