我正在使用Meteor.js。
构建朋友列表管理系统为此,我有一个用户列表,我可以单击以邀请他们在我的朋友列表中。
这是我的代码。我在e.target.id和e.target.name中分别获得了另一个用户_id和profile.name。
Template.talkers.events({
"click .addTalker": function(e, tmpl){
/* Checking if there already is a pending request */
var bool = false;
for(var i = 0; i < Meteor.user().profile.friends.length; i++){
if(Meteor.user().profile.friends[i].id == id){
bool = false;
}else{
bool = true;
break;
}
}
/* If there isn't */
if(!bool){
/* I add the other user in my friend list with a pending status */
Meteor.users.update({_id: Meteor.userId()}, {
$push: {'profile.friends': {
id: e.target.id,
status: 0
}
}
});
/* Then, I add the request on the other user profile */
Meteor.users.update({_id: e.target.id}, {
$set: {'profile.friends': {
id: Meteor.userId(),
status: 2
}
}
});
/* And I create a new notification for the other user */
var notifId = Meteor.users.findOne({_id: e.target.id}).profile.notifications.length;
console.log(notifId + 1);
Meteor.users.update({_id: e.target.id}, {
$push: {'profile.notifications': {
id: (notifId + 1),
type: 1,
img: null,
link: "/profile"},
}
});
alert("An invitation has been sent to " + e.target.name)
}
}
});
}
问题是,如果我在朋友列表中正确添加其他用户,并以待处理状态添加,我会在另一个用户的两个更新中获得两次相同的错误。目前,我将所有用户的个人资料向客户端发表_ID和一些信息。我想问题来自于我可能不会从客户端重新启动另一个用户配置文件的事实,而只是为了阅读它。然后,我想我应该使用方法调用来进行服务器?
您可以确认我的问题,还是向我解释如何进行?
谢谢
是的,其他用户缺乏更新权限可能是这里的问题。从那里,您有两个选择:
- 使用服务器方法为您进行更新,就像您说的那样(推荐)
- 如果您信任用户(在我的拙见中,在大多数情况下:不要),则可以在您想要的条件下允许
Meteor.users
更新权限。然后,您应该能够将更新电话保留在客户端中。