简体   繁体   English

MeteorJS用户配置文件对象属性不存在

[英]MeteorJS user profile object property doesn't exist

Hey guys I'm trying to make it so that the user profile of the default user package has the property of user.profile.friendList which is going to just be a quote unquote foreign key to the friendList collection where I store the user's friends. 大家好,我正在尝试使默认用户包的用户配置文件具有user.profile.friendList属性,该属性将只是对我存储用户朋友的friendList集合进行引用的无引号外键。 It's saying that the property friendList of undefined doesn't exist, though. 就是说,undefined的属性friendList不存在。

This is the serverside JS I have relating to it: 这是我与此有关的服务器端JS:

friends = new Mongo.Collection("friends");

Accounts.onCreateUser(function(options, user) {
        // We're enforcing at least an empty profile object to avoid needing to check
        // for its existence later.
        user.profile = options.profile ? options.profile : {};
        friends.insert({owner:Meteor.userId()});
        user.profile.friendList = friends.findOne({owner:Meteor.userId()})._id;
        return user;
    });

Meteor.publish("friendsPub",  function(){
        list = this.userId.profile.friendList;
        if(list) return friends.findOne({owner:list});      
    });

and here is the client side js that interacts with it: 这是与之交互的客户端js:

Template.login.helpers({
    getFriends: function(){
        if(Meteor.userId()){
            Meteor.subscribe("friendsPub"); 
            return friends.find().fetch();
        }
    },

and all that is supposed to do is create a user with a friends id as the property friendList of the user profile. 而所有要做的就是创建一个具有好友ID的用户,作为用户个人资料的属性friendList。 Then it uses that to grab the users listed in the friends collection. 然后,它使用它来获取friends集合中列出的用户。 I realize that it would only display the id of the users in the friendsList, but I wanted to get this up and running before I made it do the actual friends usernames. 我意识到它只会在friendsList中显示用户的ID,但是我想让它启动并运行实际的朋友用户名。

Meteor.userId is null inside of onCreateUser (the account hasn't been created yet). Meteor.userIdonCreateUser内部为null (尚未创建帐户)。 One possibility is to examine the friend list inside of onLogin . 一种可能性是检查onLogin内部的朋友列表。 Give something like this a try: 尝试以下操作:

Accounts.onLogin(function(data) {
  var user = Meteor.users.findOne(data.user._id);
  if(_.isEmpty(user.profile.friendList)) {
    // insert stuff here
  }
});

Alternatively, you could have the client call a method like this: 或者,您可以让客户端调用如下方法:

Meteor.methods({
  addFriendsList: function() {
    var user = Meteor.users.findOne(this.userId);
    if(_.isEmpty(user.profile.friendList)) {
      // insert stuff here
    }
  }
});

in the callback from Accounts.createUser . Accounts.createUser的回调中。

A third alternative is to just mark the user as "new" and sweep over all new users in a cron job. 第三种选择是仅将用户标记为“新”,然后在cron作业中扫描所有新用户。 See this issue for more details. 有关更多详细信息,请参见此问题

Also note that friendsPub needs to return a cursor and not a document (you want your publishers to call find and not findOne ). 还要注意, friendsPub需要返回一个游标而不是一个文档(您希望发布者调用find而不是findOne )。

Like stated by others the userId does not exist yet inside the onCreateUser. 就像其他人所说的那样,userId在onCreateUser中尚不存在。 I suggest you take the email to add to a universal list or better yet, simply add the empty friendlist to the profile and fill that with other userId's. 我建议您将电子邮件添加到通用列表或更好的列表中,只需将空的朋友列表添加到配置文件中,然后用其他userId填充即可。 Below code is how you correctly add your attributes to the user profile. 下面的代码是如何将属性正确添加到用户配置文件的方式。

Accounts.onCreateUser(function(options, user){
    console.log(options.email); // possible available identifier

    var customProfile = new Object();
    customProfile.friendList= [];

    user.profile = customProfile;
    return user;
});

I think that publications should return a cursor, as what is documented in 我认为出版物应该返回一个游标,因为

http://docs.meteor.com/#/full/meteor_publish . http://docs.meteor.com/#/full/meteor_publish

The friendsPub publication returns a single document using findOne . friendsPub发布使用findOne返回单个文档。 To quote: 报价:

If a publish function does not return a cursor or array of cursors, it is assumed to be using the low-level added/changed/removed interface, and it must also call ready once the initial record set is complete. 如果发布函数未返回游标或游标数组,则假定该发布函数正在使用低级添加/更改/删除的接口,并且在初始记录集完成后,它还必须调用ready。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM