2

我看不到我的用户的任何个人资料信息,知道为什么吗?

服务器 :

Meteor.publish("userData", function () {
    return Meteor.users.find({_id: this.userId},
        {fields: {'profile': 1}});
});
Meteor.publish("allUsers", function () {
    //TODO: For testing only, remove this
    return Meteor.users.find({}, {fields: {'profile': 1}});
});

客户 :

Meteor.autosubscribe(function () {
    Meteor.subscribe('allUsers',null , function() { console.log(Meteor.users.find().fetch()) });
    Meteor.subscribe('userData', null, function() { console.log(Meteor.user())});
});

....

Accounts.createUser({email:email,password:password, profile: {name: name}},function(error){
    ...
});

我的控制台输出一个对象,第一个对象只有 _id 和电子邮件,第二个对象未定义。配置文件信息(在我的情况下为名称)似乎有效,因为在我的 server.js 中,我有一个可以正常工作的名称验证:

Accounts.onCreateUser(function(options, user) {
    if(options.profile.name.length<2)
        throw new Meteor.Error(403, "Please provide a name.");
    return user;
});

我错过了什么吗?

谢谢!

4

3 回答 3

3

使用多个订阅时,只有第一个订阅很重要。第二个订阅,如果包含相同的集合被忽略,因为它与第一个冲突。

不过,您可以这样做:

服务器:

var debugmode = false; //set to true to enable debug/testing mode
Meteor.publish("userData", function () {
    if(debugmode) {
        return Meteor.users.find({}, fields: {'profile': 1}});
    }
    else
    {
        return Meteor.users.find({_id: this.userId},{fields: {'profile': 1}});
    }
});

客户:

Meteor.autosubscribe(function () {
    Meteor.subscribe('userData', null, function() { console.log(Meteor.user()); console.log(Meteor.users.find({}).fetch());});
});
于 2013-01-28T10:42:46.580 回答
3

发现问题:

在 onCreateUser 函数中,我需要将选项中的配置文件信息添加到用户对象,所以我的函数应该如下所示:

Accounts.onCreateUser(function(options, user) {
    if(options.profile.name.length<2)
        throw new Meteor.Error(403, "Please provide a name.");
    if (options.profile)
    user.profile = options.profile;
    return user;
});
于 2013-01-28T15:44:27.733 回答
1

这是我正在使用的解决方法,放置在 ~/server/createAccount.js 我遇到的问题是我会在配置文件未定义的情况下出现错误。这似乎通过在创建帐户时创建配置文件来解决问题。

希望这是有用的。在 github 问题评论中找到它,在下面的评论中:

// BUGFIX via https://github.com/meteor/meteor/issues/1369 @thedavidmeister
// Adds profile on account creation to prevent errors from profile undefined on the profile page
Accounts.onCreateUser(function(options, user) {
  user.profile = options.profile ? options.profile : {};
  return user;
});
于 2014-01-06T12:33:52.263 回答