简体   繁体   English

流星:将数据输入到新集合

[英]Meteor: Input data to a new collection

I try to insert some elements into a Collection. 我尝试将一些元素插入集合中。 It is my first attempt to use Meteor/MongoDB. 这是我第一次使用Meteor / MongoDB。 So I don't see, what I'm doing wrong with this. 所以我看不到我在做什么错。 When I try so save a new title, I get the error 当我尝试保存新标题时,出现错误

"Access denied. No allow validators set on restricted collection for method 'insert'. [403]" “访问被拒绝。在方法“插入”的受限集合上未设置允许验证器。[403]”

Template: 模板:

<template name="casuistry">
    <div class="create">
        <form>
            <input type="text" name="title" placeholder="Title">
            <button>Add</button>
        </form>
    </div>
</template>

collections.es6.js collections.es6.js

Casuistry = new Mongo.Collection('casuistry')
Casuistry.attachSchema(new SimpleSchema({
  title: {type: String},
  caseNumber: {type: Number, defaultValue: 0},
  htmlContent: {type: String, defaultValue: '[]'},
}))

casuistry.es6.js casuistry.es6.js

Template.casuistry.events({
  'submit .create form' (e, t) {
    var title = t.val('title')
    Casuistry.insert({title}, (err, _id) => {
      if (err) {
        console.log(err)
        return
      }
      t.val('title', '')
      Router.go('casuistry', {_id})
    })
    return false
  }
})

You're trying to insert into the collection from the client-side. 您正在尝试从客户端插入集合中。 Without the insecure package you have to first define what is allowed and what is not allowed server-side - to make sure that users don't just insert/remove/update whatever they want. 如果没有insecure程序包,则必须首先定义服务器端允许和不允许的内容-以确保用户不只是插入/删除/更新他们想要的任何内容。

You can either use allow / deny or use methods (highly recommended!). 您可以使用allow / deny或使用methods (强烈建议!)。 I'm not going to go too deep on methods vs. allow/deny, there are good blog posts available on this topic , but in general: allow/deny is hard to get right and you just have more control in a method. 我不会对方法与允许/拒绝进行深入探讨, 关于此主题有很多不错的博客文章 ,但总的来说:允许/拒绝很难正确,您只能对方法进行更多控制。

Something to note is that you can also define client-side collections by passing null into the constructor: 需要注意的是,您还可以通过将null传递给构造函数来定义客户端集合:

var clientCollection = new Mongo.Collection(null);

In this case, you'd use the Collection exactly like you did - because it's a client-side only collection, there is no reason to worry about someone tampering with your "real" data. 在这种情况下,您将像使用集合一样使用它-因为它是仅客户端集合,因此没有理由担心有人会篡改“真实”数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM