gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 09:23:05 27 4
gpt4 key购买 nike

嘿伙计们,我正在尝试使默认用户包的用户配置文件具有 user.profile.friendList 属性,这将只是我存储的friendList集合的引用外键用户的 friend 。不过,这表明 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();
}
},

要做的就是创建一个用户,其 friend ID 作为用户配置文件的 friend 列表属性。然后它使用它来获取 friend 集合中列出的用户。我意识到它只会显示 FriendsList 中用户的 ID,但我想在让它执行实际的 friend 用户名之前先启动并运行它。

最佳答案

onCreateUser 内的

Meteor.userIdnull (该帐户尚未创建)。一种可能性是检查 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 的回调中.

第三种选择是仅将用户标记为"new"并在 cron 作业中扫描所有新用户。请参阅this issue了解更多详情。

另请注意,friendsPub 需要返回光标而不是文档(您希望发布商调用 find 而不是 findOne)。

关于javascript - MeteorJS 用户配置文件对象属性不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31778495/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com