Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 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 Reply

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});
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...