【问题标题】:failed: Access denied on meteor collection失败:访问流星收集被拒绝
【发布时间】:2016-02-23 07:54:25
【问题描述】:

此 Meteor 应用已删除不安全和自动发布功能,并添加了帐户密码。
它使用 Accounts.createUser({username: someName, password: somePwrd}); 可以在 mongo 提示符下进行验证。

我正在尝试Tasks1.insert(params); 并被拒绝访问

我不知道为什么在浏览器控制台上更新和插入会被拒绝访问。请告诉我为什么以及如何解决它?谢谢

//both.js
Tasks1 = new Mongo.Collection('tasks1');
/////////////////////////////////////////////////////

//server.js
Meteor.publish('tasks1', function(){
  return Tasks1.find({userId: this.userId});
});

Meteor.methods({
  logMeIn: function(credentials) {
    var idPin = credentials[0] + credentials[1];
    Accounts.createUser({username: idPin, password: credentials[1]});
  }
});

Meteor.users.allow({
  insert: function (userId, doc) {
   console.log(userId);
   //var u = Meteor.users.findOne({_id:userId});
  return true;
}
});
/////////////////////////////////////////////////////  

//client.js
Template.login.events({
   'click #logMe': function() {
   var credentials = [$('#id').val(), $('#pin').val()];
   Meteor.call('logMeIn', credentials, function(err, result) {
    if (result) {
      console.log('logged in!');
    }
  });
 }
});
Template.footer.events({
  'click button': function () {
    if ( this.text === "SUBMIT" ) {
      var inputs = document.getElementsByTagName('input');
      for (var i = 0; i < inputs.length; i++) {
       var params = {};
       params[inputs[i].name] = inputs[i].value;
       Tasks1.insert(params);  //<<<<<<----------------------
    }
  }
 }
});

【问题讨论】:

  • 文件是否也在适当的目录中(服务器/客户端等)?您实际上是否将用户密码作为用户名的一部分以明文形式存储在数据库中?
  • 两个问题都是:)
  • server.js 文件相对于项目根目录的路径是什么?
  • appName/server/server.js
  • 那我不明白为什么会发生这样的事情。可能是发布的代码中未提及的另一个副作用。您可以创建一个最小的可重现示例并发布您遇到的错误吗?另外,为什么每次有人提交表单时都要创建一个用户,为什么要以明文形式存储密码?

标签: meteor


【解决方案1】:

更新: 由于您已经编辑了您的问题并添加了 Tasks1.insert(params); 正在收到拒绝访问消息,您应该在 Tasks 集合上添加 allow 规则,而不是 Meteor.users 集合。

Tasks.allow({
    insert: function (userId, doc) {
           return true;
    },
    update: function (userId, doc, fieldNames, modifier) {
           return true;
    },
    remove: function (userId, doc) {
           return true;
    }
});

如果Accounts.createUserMeteor.users 上没有allow 规则的情况下工作,请删除它们,因为它可能允许用户从客户端本身插入/删除其他人。

更新结束。

由于您删除了insecure,您需要添加allow/deny 规则,用于从集合中插入、更新或删除文件。

Meteor.users.allow({
    insert: function (userId, doc) {
           //Normally I would check if (this.userId) to see if the method is called by logged in user or guest
           //you can also add some checks here like user role based check etc.,
           return true;
    },
    update: function (userId, doc, fieldNames, modifier) {
           //similar checks like insert
           return true;
    },
    remove: function (userId, doc) {
           //similar checks like insert
           return true;
    }
});

查看API documentation了解更多详情。

【讨论】:

  • 您应该在 Tasks.allow 中插入一个 fetch 条目。您可以查看随附的 API 文档了解更多信息。
  • 短格式:Tasks.allow({ insert: (userId, doc) =&gt; true, update: (userId, doc, fieldNames, modifier) =&gt; true, remove: (userId, doc) =&gt; true, });
【解决方案2】:

像这样定义 Meteor.methods 将为服务器和客户端定义它。这意味着您将尝试创建一个用户 TWICE,一次在服务器上(有效的那个),另一次在客户端上。客户端无权插入用户文档,因此您收到此错误。

有两种选择:

1:只在服务器上定义方法,方法是用if(Meteor.isServer)包围它或者把它放在一个名为“server”的文件夹中

2:保持原样,不会造成伤害,但会在控制台中不断显示错误。

我确信有第 3 种甚至第 4 种解决方案,但我会使用这两个。

【讨论】:

  • 方法是在server.js里面定义的,你说的是集合吗?
  • 这意味着你没有告诉我们所有...我看到你更新了问题并且你得到了答案,所以我不会更新我的。
猜你喜欢
  • 1970-01-01
  • 2020-11-24
  • 2017-05-17
  • 1970-01-01
  • 2020-11-16
  • 1970-01-01
  • 1970-01-01
  • 2012-09-27
  • 1970-01-01
相关资源
最近更新 更多