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 條件判斷和循環
下一篇 暴風科技暴漲之后或將一地雞毛