gpt4 book ai didi

javascript - Meteor js,如何获得更新整个客户端文档的副作用

转载 作者:可可西里 更新时间:2023-11-01 09:59:11 24 4
gpt4 key购买 nike

我有一个从客户端调用的 Meteor 方法,它依次更新单个文档中的所有字段。在下面的最后一行代码中,我尝试立即在客户端上运行相同的更新以获得直接的副作用(它假设更新将通过)。问题是我得到:

update failed: Access denied. In a restricted collection you can only update documents, not replace them. Use a Mongo update operator, such as '$set'

尝试这样做时。我不想更新单个字段,我想更新整个文档。有没有办法正确地做到这一点?

entry = {
title: title
text: text
tags: entry.tags
mode: $('#mode').val()
file_ids: entry.file_ids
}

eid = Session.get('entryId')
entry._id = eid if eid

context = Session.get('context')
Meteor.call('saveEntry', title, entry, context)
Entries.update({_id: entry._id}, entry)

最佳答案

做类似的事情:

Entries.update({_id: entry._id}, { $set: entry })

典型的更新请求在表单上:

Collection.update(
<query>,
<update>
)

来自 Mongodb 文档,关于使用常规 key:value 对象进行更新:

Replace Document Entirely

If the document contains only field:value expressions, then:

  • The update() method replaces the matching document with the document. The update() method does not replace the _id value. For an example, see Replace All Fields.
  • update() cannot update multiple documents.

以及关于更新特定字段,使用 $set$inc 等:

Update Specific Fields

If the document contains update operator expressions, such as those using the $set operator, then:

  • The document must contain only update operator expressions.
  • The update() method updates only the corresponding fields in the document. For an example, see Update Specific Fields.

此外,据我了解,从您的错误消息 Meteor 不允许从客户端完全替换整个文档。因此,一种选择是使用 $set 运算符并设置所有值,在本例中是整个文档本身。在将文档传递给 $set 运算符之前,您通常可以跳过将 _id 键添加到文档中。

关于javascript - Meteor js,如何获得更新整个客户端文档的副作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23897612/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com