【发布时间】:2015-05-04 22:41:51
【问题描述】:
我正在使用 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和一些信息发布到客户端。我猜这个问题来自这样一个事实,我可能不会被允许从客户端重写另一个用户配置文件,而只是阅读它。然后,我想我应该通过方法调用在服务器端进行操作?
您能否确认我的问题,或者向我解释如何进行?
谢谢你
【问题讨论】: