Meteor.js - 更新用户配置文件最方便的方法是什么?

时间:2014-11-14 15:26:13

标签: meteor

我发现我广泛使用用户个人资料。我希望能够做到这样的事情:

Meteor.user().profile.some_setting = 'something';
Meteor.user().update();

更新用户个人资料的最便捷方式是什么?

1 个答案:

答案 0 :(得分:4)

Meteor.user()是一个文档,而不是一个游标。它实际上是Meteor.users.findOne(this.userId)的别名。

您可以通过方法调用(服务器)或直接在客户端上执行此操作。

方法调用方式:

//server code
Meteor.methods({
  updateProfile : function(newProfile) {
    if(this.userId)
      Meteor.users.update(this.userId, {$set : { profile : newProfile }});
  }
});

在客户端:

Meteor.call('updateProfile', myNewProfile);

我建议通过服务器方法这样做,因为代码在更干净的环境中运行。

如果您想直接在客户端上执行此操作:

Meteor.users.update(Meteor.userId(), {$set : {profile : myNewProfile}});

Meteor.userId()Meteor.user()._id)的别名 More infos on the doc!

相关问题