meteor 允许规则

标签 meteor rules

我有一个关于 meteor 派对示例的问题。

如果我调用此代码:

Parties.allow({
insert: function () {
    return true;
},

remove: function (){
    return true;    
},

update: function() {
    return true;    
}

});

每个人都可以插入、删除和更新。
示例中的代码是
Parties.allow({
 insert: function (userId, party) {
    return false; // no cowboy inserts -- use createPage method
 },
 update: function (userId, parties, fields, modifier) {
    return _.all(parties, function (party) {
    if (userId !== party.owner)
       return false; // not the owner

  var allowed = ["title", "description", "x", "y"];
  if (_.difference(fields, allowed).length)
    return false; // tried to write to forbidden field

  // A good improvement would be to validate the type of the new
  // value of the field (and if a string, the length.) In the
  // future Meteor will have a schema system to makes that easier.
     return true;
   });
 },
 remove: function (userId, parties) {
   return ! _.any(parties, function (party) {
     // deny if not the owner, or if other people are going
     return party.owner !== userId || attending(party) > 0;
   });
 }
});

所以我的问题是变量 useriD 和 party 在这一行的位置,例如
 insert: function (userId, party) {

被定义?
这些是我在方法中调用的变量吗
 Meteor.call("createParty", variable1, variable2)

?但这没有意义,因为客户端调用
 Meteor.call('createParty', {
    title: title,
    description: description,
    x: coords.x,
    y: coords.y,
    public: public
  }

我希望有人可以向我解释允许功能吗?谢谢!

最佳答案

要了解允许/拒绝,您需要了解 userId 的位置和 doc参数来自。 (就像在任何函数定义中一样,实际参数名称无关紧要。)只看 Party 插入示例:

Parties.allow({

  insert: function (userId, party) {
     return false; // no cowboy inserts -- use createPage method
  }

});
party参数是正在插入的文档:
Parties.insert(doc);
userId如果您使用的是 Meteor Accounts 身份验证系统,则参数会自动设置。否则,您必须自己在服务器上进行设置。你是怎样做的?

通常,您可以使用 Meteor.call() 从客户端调用服务器上的代码。 .由于没有内置 API 来设置 userId(帐户除外),因此您必须编写自己的(在您的服务器代码中):
Meteor.methods({

   setUserId: function(userId) {
       this.setUserId(userId);
   }

});

然后你可以在你的客户端代码的任何地方这样调用它:
Meteor.call('setUserId', userId);

关于 meteor 允许规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14497226/

相关文章:

mongodb - 如何在Meteor中动态给出集合字段名称?

javascript - 使用对等库 :aws-sdk 将文件从 Meteor 服务器推送到 AWS S3

shell - 错误 : EACCES, 取消链接...

javascript - 单 View 页面永久链接为空,带有 Iron Router

database - Postgresql - 如果记录不存在则以干净的方式插入记录,如果存在则更新

comparison - 如何比较两个规则列表?

meteor - 在 Meteor 中执行 DOM 操作的正确方法是什么?

java - 在 Fortify 中编写规则

java - "JavaSonarQube"和 "Java Common Sonarqube"的分析器

testing - 我们可以在拉丁语言中使用什么样的分割规则来编写测试用例?