Meteor Mongo插入失败 - 访问被拒绝

Ale*_*lex 12 database mongodb node.js meteor

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

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

if (Meteor.is_server) {
People.insert({name: 'Bob'});
}
if (Meteor.is_client) {
People.insert({name: 'Bob'});
}
Run Code Online (Sandbox Code Playgroud)

Ale*_*lex 20

因为您正在使用auth,所以您必须允许或拒绝尝试执行插入,更新,删除和提取的客户端.要解决此特定问题,必须添加Collection.allow()以使客户端的插入工作.

if(Meteor.is_server) {

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

}
Run Code Online (Sandbox Code Playgroud)

  • 作为缺乏经验的读者的旁注:此答案中的代码段对您的案例可能不安全.此片段允许_any_客户端更新相关集合(即People).如果您使用的是Meteor的新帐户API(http://docs.meteor.com/#accounts_api),则需要返回对已登录用户的检查(例如,返回userId && doc.owner === userId,其中所有者为您的集合的获取属性).请参阅http://docs.meteor.com/#allow中的"示例"部分 (5认同)

小智 8

在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);
  }
});
Run Code Online (Sandbox Code Playgroud)