Meteor JS中是否允许收集技术指南



我需要了解Meteor JS中collection.allow()的技术含义。我研究过Meteor文档,但没有完全理解。所以你能用下面的代码解释一下下面的术语吗。

  • 什么是文档?

  • 如何检查Posts.allow()是否返回true/false?

  • 如何调用以下方法,如insert、update&点击按钮时删除?

  • 如何编写查询以插入、更新&在Meteor JS中使用以下方法删除?

  • 如何检查多个人允许插入,更新&删除查询?

你能就以上事情给我一些建议吗?

Posts = new Meteor.Collection("posts");
Posts.allow({
insert: function (userId, doc) {
// the user must be logged in, and the document must be owned by the user
return (userId && doc.owner === userId);
},
update: function (userId, doc, fields, modifier) {
// can only change your own documents
return doc.owner === userId;
},
remove: function (userId, doc) {
// can only remove your own documents
return doc.owner === userId;
},
fetch: ['owner']
});

这些方法用于验证客户端请求的插入/更新/删除。如果客户端调用CCD_ 1。服务器将使用Posts.allow来验证这是否真的可以发生。直接回答您的问题:

what is the doc?

这些方法中的doc是客户端传入的文档。在我上面的示例中,它将是somePost

How to check Posts.allow() is return true/false?

Posts.allow()将检查用户是否可以插入帖子,如果可以则返回true,如果不能则返回false(这是您的责任)。在您的示例中,必须有一个有效的userId,并且文档的所有者必须是当前登录的用户。由于doc是一个JSON对象,因此在本例中它必须有一个owners字段。如果你总是返回false,那么没有客户能够创建帖子。如果您总是返回true,那么任何插入帖子的请求都将被接受。

How to call the below methods like insert,update & remove when ever clicks a button?

实际上,您从来没有直接调用过这些方法。当客户端尝试插入/更新/删除帖子时,会调用它们。

How to write queries to insert, update & remove using the below methods in Meteor JS?

同样,您从来没有直接调用过这些,但当您执行Posts.insert(somePost)时,它将自动尝试根据Posts.insert(somePost)0allow方法进行验证。如果接收到true,则插入帖子。如果它收到一个false,它将抛出一个异常。

How to check more than one person allows to insert,update & remove queries?

不完全确定这是什么意思,但如果有两个人登录,并且他们都试图插入帖子,则可以在方法中给定userId字段的情况下对他们进行唯一验证。


更新:

我将详细阐述你的评论问题。文档对象上只有一个owner属性。传入的文档可能看起来像这样(简化):

doc = {
"name":"My Important Document",
"description": "This is a great document.",
"createdOn": 1394043417621,
"owner": b8QsgX3awg7E9DMKs
}

所以doc.owner会给你文档所有者的id。然后你可以将其与传递的userId进行比较,看看他们是否是同一个人。

最新更新