javascript - Meteor js .allow 列表未显示

标签 javascript node.js web-applications meteor

我正在尝试在我正在构建的应用程序中实现 meteor 的 .allow 部分。在引入它之前,列表显示用户输入的评论,现在评论只是闪烁一秒钟然后消失。不过,这些评论仍在添加到收藏中。 谁能告诉我我做错了什么,我对此很陌生。

主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 是当前登录用户的 _id。插入问题时需要插入 owner。这是唯一的方法(que.owner === userId 将返回 true):

Questions.insert({
    owner: Meteor.userId(),
    body: $body.val(),
    score: $score,
    created_at: Date()
});

确保只有登录用户才有机会提出问题。通过隐藏按钮或在插入所有内容之前进行检查:

if(!Meteor.userId()) {
    alert("You need to be logged in to post a question");
    return;
}

关于javascript - Meteor js .allow 列表未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16402683/

相关文章:

azure - 如何将 Web 应用程序的部署槽与虚拟应用程序交换

java - 在 Spring MVC 之上使用 Spring WebFlow 什么时候有意义?

javascript - Chrome 版本 56 更改事件

javascript - Youtube/vimeo iframe 无法在 Firefox 中播放

javascript - Google Maps InfoWindow -(单击)触发 Angular 2 函数

javascript - 是否可以在 NPM 包中为浏览器和服务器(NodeJS)定义不同的位置?

node.js - 如何以驼峰式风格返回sequelize查询的结果集?

javascript - 绕过 iOS Safari/任何移动浏览器上的弹出窗口拦截器

javascript - API 在 Postman 中有效,但在浏览器中无效

javascript - Node.js 括号语法