【问题标题】:Meteor Mongo Insert Failed -- Access DeniedMeteor Mongo 插入失败——访问被拒绝
【发布时间】:2012-09-27 05:40:09
【问题描述】:

第一个插入工作正常,但第二个在控制台中给出“插入失败:403 -- 访问被拒绝”。自动订阅已启用,我在 auth 分支上。如何设置我的代码,以便我拥有一个客户端可以写入的服务器 MongoDB?

People = new Meteor.Collection('people'); 

if (Meteor.is_server) {
People.insert({name: 'Bob'});
}
if (Meteor.is_client) {
People.insert({name: 'Bob'});
}

【问题讨论】:

  • 是的,我在 auth 分支上。
  • 好的,我猜是因为我没有插入验证器...
  • 没错,你需要打电话给People.allow

标签: node.js mongodb meteor database


【解决方案1】:

由于您使用的是身份验证,因此您必须允许或拒绝尝试执行插入、更新、删除和提取的客户端。要解决此特定问题,您必须添加 Collection.allow() 以让客户端的插入工作。

if(Meteor.is_server) {

  People.allow({
    'insert': function (userId,doc) {
      /* user and doc checks ,
      return true to allow insert */
      return true; 
    }
  });

}

【讨论】:

  • 作为没有经验的读者的旁注:此答案中的代码 sn-p 对于您的情况可能不安全。这个 sn-p 允许 any 客户端更新相关的集合(即 People)。如果您使用 Meteor 的新 Accounts API (docs.meteor.com/#accounts_api),则需要返回对已登录用户的检查(例如,返回 userId && doc.owner === userId,其中 owner 是您的集合的获取属性)。请参阅docs.meteor.com/#allow中的示例部分
  • 我必须把它放在 is_server 命名空间之外才能工作。
【解决方案2】:

在 Collection People 中使用方法 allow()。此方法分配访问 CRUD。

function adminUser(userId) {
  var adminUser = Meteor.users.findOne({username:"admin"});
  return (userId && adminUser && userId === adminUser._id);
}
Lugares.allow({
  insert: function(userId, lugar){
    return adminUser(userId);
  },
  update: function(userId, lugares, fields, modifier){
    return adminUser(userId);
  },
  remove: function (userId, docs){
    return adminUser(userId);
  }
});

【讨论】:

  • 为什么是“卢加雷斯”?也许应该是“人”?
  • 这个答案和问题不是真的相关吗?
  • 为什么这被否决了?它显示了您需要为其添加处理程序的三个允许函数。此外,此代码需要在客户端和服务器上运行,因此更合适。 (客户端预先计算调用结果并跳过服务器往返。
【解决方案3】:

我从项目中删除 insecure 包后遇到此错误。

meteor remove insecure

以下解决了我的问题。

Posts = new Meteor.Collection('posts');

Posts.allow({
  insert: function(userId, doc) {
    // only allow posting if you are logged in
    return !! userId;
  }
});

【讨论】:

【解决方案4】:

如果你只有simple-todos教程之类的测试项目,可以通过添加不安全的包来解决

meteor add insecure

【讨论】:

    猜你喜欢
    • 2020-11-24
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多