Meteor 提供了两个 MongoDB 数据库:1个客户端缓存数据库和服务器上的1个 MongoDB 数据库。当1个用户更改1些数据时(例如通过单击 Save),在阅读器中运行的 JavaScript 代码会更新本地 MongoDB 中的相应的数据库项,然后向服务器发出1个 DDP 要求。该代码立即像操作已取得成功那样继续运行,由于它不需要等待服务器回复。与此同时,服务器在后台更新。如果服务器操作失败或返回1个意外结果,那末客户端 JavaScript 代码会根据从服务器新返回的数据立即进行调剂。
昨天写代码的时候发现,发现有1个update操作总是失败,而也没有甚么毛病提示,只显示个error。找了很久找不到问题出在哪。
然后去读官方文档,它是这样写的:
文档中关于update的说明:
collection.update(selector, modifier, [options], [callback]) Anywhere
Modify one or more documents in the collection
Arguments
selector Mongo selector, or object id
Specifies which documents to modify
modifier Mongo modifier
Specifies how to modify the documents
callback Function
Optional. If present, called with an error object as its argument.
Options
multi Boolean
True to modify all matching documents; false to only modify one of the matching documents (the default).
再查看1次我写的代码,应当没错的。然后再mongodb的命令行工具上试试履行一样语句也是可以的,因而我就觉得是Meteor的相干设置问题。
再往文档中细看,发现问题了:
The behavior of update differs depending on whether it is called by trusted or untrusted code. Trusted code includes server code and method code. Untrusted code includes client-side code such as event handlers and a browser’s JavaScript console.
Trusted code can modify multiple documents at once by setting multi to true, and can use an arbitrary Mongo selector to find the documents to modify. It bypasses any access control rules set up by allow and deny.
Untrusted code can only modify a single document at once, specified by its _id. The modification is allowed only after checking any applicable allow and deny rules.
原来为了安全性,Meteor限制了在客户端下默许的对数据库的操作,对insert,update,remove等操作的selector只能为 _id,并且不能使用mutil等,所以我写的那条update语句的毛病在于selector用了_id外的条件。所以看来解决方法有两个:
文档中关于allow的说明:
collection.allow(options) Server
Allow users to write directly to this collection from client code, subject to limitations you define.
Options
insert, update, remove Function
Functions that look at a proposed modification to the database and return true if it should be allowed.
因而写了相干的allow规则, 让他在某个情况下return true就能够了
上一篇 python 5 条件判断和循环
下一篇 暴风科技暴涨之后或将一地鸡毛