代码之家  ›  专栏  ›  技术社区  ›  Fred J.

失败:流星收集拒绝访问

  •  5
  • Fred J.  · 技术社区  · 10 年前

    此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);  //<<<<<<----------------------
        }
      }
     }
    });
    
    2 回复  |  直到 10 年前
        1
  •  8
  •   Kees de Kooter chotai.mit    6 年前

    更新: 因为你已经编辑了你的问题并添加了 Tasks1.insert(params); 正在收到拒绝访问的消息,您应该添加 allow 关于的规则 Tasks 收集而不是 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.createUser 没有 允许 关于的规则 流量计.用户 然后请删除它们,因为它可能允许用户从客户端本身插入/删除其他内容。

    更新结束。

    自从您删除 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 了解更多详细信息。

        2
  •  0
  •   Salketer    10 年前

    定义流星。这样的方法将为服务器和客户端定义它。这意味着您将尝试两次创建用户,一次在服务器上(工作的),另一次在客户端上。客户端无权插入用户文档,因此您会收到此错误。

    有两个选项可供选择:

    1: 仅通过以下方式在服务器上定义方法 if(Meteor.isServer) 或将其放在名为“服务器”的文件夹中

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

    我确信有第三种可能是第四种解决方案,但我会使用这两种。