嘿,伙计们,我对 Meteor 帐户 api 有疑问。我试图只让登录的用户更改他们自己的列表,而不影响其他用户列表这是我的代码:客户端:
Meteor.subscribe('Categories');
Meteor.autosubscribe(function() {
Meteor.subscribe("listdetails",
Session.get('current_list'));
});
'keyup #add-category': function (e,t){
if (e.which === 13)
{
var catVal = String(e.target.value || "");
if (catVal)
{
lists.insert({Category:catVal,owner:this.userId});
Session.set('adding_category', false);
}
}
},
服务器端:
Meteor.startup(function () {
Meteor.publish("Categories", function() {
return lists.find({owner:Meteor.userId},{fields:{Category:1}});
});
Meteor.publish("listdetails", function(category_id){
return lists.find({_id:category_id});
});
});
双方(客户端和服务器):
lists = new Meteor.Collection("Lists");
/*function adminUser(userId) {
var adminUser = Meteor.users.findOne({username:"boazhoch"});
return userId && adminUser && userId === adminUser._id;
} */
function adminUser(userId) {
var adminUser = Meteor.users.findOne({username:"admin"});
return (userId && adminUser && userId === adminUser._id);
}
lists.allow({
insert: function (userId, doc) {
// the user must be logged in, and the document must be owned by the user
return (adminUser(userId) || userId && doc.owner === userId);
},
update: function(userId, docs, fields, modifier){
return adminUser(userId) || _.all(docs, function(doc) {
return doc.owner === userId;
});
},
remove: function (userId, docs){
return adminUser(userId) || _.all(docs, function(doc) {
return doc.owner === userId;
});
},
fetch: ['owner']
});
您可以清楚地看到,当使用管理员登录但未登录时,屏幕是相似的(不是我想要的结果),并注意到 this.userId 是“未定义”,这是有线的,这就是我使用 Meteor.userId 的原因。