meteorjs。允许列表不显示
我正在尝试在我正在构build的应用程序中实现meteor的.allow部分。 在介绍之前,一个列表显示用户input的注释,现在注释只是闪烁一下,然后消失。 尽pipe如此,评论仍然被添加到collections。 谁能告诉我我做错了什么,我对此很新。
主要的js文件:
if (Meteor.isClient) { Meteor.startup(function () { Meteor.subscribe("ques"); }); Template.compose.events({ 'submit form': function (event) { var $body = $('#que-body'); var $score = 1; event.preventDefault(); Questions.insert({ body: $body.val(), score: $score, created_at: Date() }); $body.val(''); } }); Template.question.selected = function () { return Session.equals("selected_question", this._id) ? "selected" : ''; }; Template.question.events({ 'click': function () { Session.set("selected_question", this._id); } }); Template.question.que = function(){ return Questions.findOne(Session.get("selected")); }; // Deals with up-vote, down-vote, remove buttons Template.list.events({ 'click .icon-thumbs-up': function(event) { Questions.update(Session.get("selected_question"), {$inc: {score: 1}}); }, 'click .icon-thumbs-down': function(event) { Questions.update(Session.get("selected_question"), {$inc: {score: -1}}); }, 'click .icon-remove': function(event) { Questions.remove(Session.get("selected_question")); } }); Template.list.questions = Questions.find({}, {sort: {score: -1, created_at: -1}}); } if (Meteor.isServer) { Meteor.startup(function () { Meteor.publish("ques", function(){ return Questions.find({}, { fields:{ } }) }); }); }
model.js文件:
Questions = new Meteor.Collection("questions"); Questions.allow({ insert: function(userId, que){ return userId && que.owner === userId; }, update: function(id, ques, fields, modifier){ return true; }, remove: function(id, que){ return id && que.owner === id; } });
你的意思是你提到的问题(你说的评论?):你的Meteor.allow
规则基本上是说question.owner
是当前login用户的_id
。 插入问题时需要插入owner
。 这是唯一的方法( que.owner === userId
将返回true
):
Questions.insert({ owner: Meteor.userId(), body: $body.val(), score: $score, created_at: Date() });
确保你确保只有login的用户有机会插入问题。 通过隐藏button或在插入所有内容之前进行检查:
if(!Meteor.userId()) { alert("You need to be logged in to post a question"); return; }