0

我的server/fixtures.js文件中有以下代码:

var userId = Accounts.createUser({
  username: "tester",
  email: "a@b.com",
  password: "foobar",
  profile: { name: "Max" }
});
var user = Meteor.users.findOne({_id: userId});
console.log(user.profile.name);

现在,当我运行meteor它时,它会记录下来undefined。我究竟做错了什么?

4

3 回答 3

2

我很确定我已经在某个地方定义了一个Accounts.onCreateUser回调来负责这个。我的错!

于 2015-08-12T11:23:35.447 回答
-1

我认为您需要使用 Meteors Publications & Subscriptions。检查此链接 以获取更多信息。

例子:

if (Meteor.isServer){
  Meteor.publish('userdata', function() {   
    if(!this.userId) return null;
    return Meteor.users.find( this.userId );
  });
}

if (Meteor.isClient){
    Meteor.subscribe('userdata');
    console.log(Meteor.user());
}
于 2015-06-10T08:12:33.307 回答
-1

所以,问题是 console.log 在服务器上不可用(该文件根据 Meteor 目录约定运行。)。如果您想登录到服务器控制台,您可以使用Meteor._debug()与 console.log 相同的工作方式。您可能还需要考虑将代码包装在一个Meteor.startup(function() {});块中,以便它在服务器启动时立即运行。

于 2015-08-12T16:38:23.607 回答