2

構築中のアプリケーションに 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;
    }
});
4

1 に答える 1

1

質問のことですか (コメントと言いましたか?): あなたの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;
}
于 2013-05-06T16:54:07.343 に答える