簡體   English   中英

MeteorJS用戶配置文件對象屬性不存在

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

大家好,我正在嘗試使默認用戶包的用戶配置文件具有user.profile.friendList屬性,該屬性將只是對我存儲用戶朋友的friendList集合進行引用的無引號外鍵。 就是說,undefined的屬性friendList不存在。

這是我與此有關的服務器端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});      
    });

這是與之交互的客戶端js:

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

而所有要做的就是創建一個具有好友ID的用戶,作為用戶個人資料的屬性friendList。 然后,它使用它來獲取friends集合中列出的用戶。 我意識到它只會在friendsList中顯示用戶的ID,但是我想讓它啟動並運行實際的朋友用戶名。

Meteor.userIdonCreateUser內部為null (尚未創建帳戶)。 一種可能性是檢查onLogin內部的朋友列表。 嘗試以下操作:

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

或者,您可以讓客戶端調用如下方法:

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

Accounts.createUser的回調中。

第三種選擇是僅將用戶標記為“新”,然后在cron作業中掃描所有新用戶。 有關更多詳細信息,請參見此問題

還要注意, friendsPub需要返回一個游標而不是一個文檔(您希望發布者調用find而不是findOne )。

就像其他人所說的那樣,userId在onCreateUser中尚不存在。 我建議您將電子郵件添加到通用列表或更好的列表中,只需將空的朋友列表添加到配置文件中,然后用其他userId填充即可。 下面的代碼是如何將屬性正確添加到用戶配置文件的方式。

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

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

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

我認為出版物應該返回一個游標,因為

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

friendsPub發布使用findOne返回單個文檔。 報價:

如果發布函數未返回游標或游標數組,則假定該發布函數正在使用低級添加/更改/刪除的接口,並且在初始記錄集完成后,它還必須調用ready。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM