gpt4 book ai didi

javascript - 无法将新数组 $push 到 Meteor Collection 中的文档中

转载 作者:行者123 更新时间:2023-11-28 00:03:08 24 4
gpt4 key购买 nike

我已经学习 Meteor 大约 3 周了,并且仍在尝试更新/查询集合。我正在尝试构建一个 Slack 克隆,并使用一组固定文档创建了以下集合:

Conversations.insert({
channel: "#defaultChannel",
createdBy: "coffeemeup",
timestamp: new Date(),
followers: ["username1", "username2"],
entries: [
{
message: "this is a message #1",
postedTime: new Date(),
author: "coffeemeup"
}]
});

我正在尝试使用下面的代码将另一个文档插入到 entries 数组中。但这不仅不起作用,还会抛出“改变对象的 [[Prototype]] 将导致您的代码运行非常缓慢......”错误。我真的很感激一些帮助!

Conversations.update({
channel: "#defaultChannel"
}, {
$push: {
entries: {
message: newMessage,
postedTime: new Date(),
author: "coffeemeup"
}
}
});

此外,我很想听到有关如何更好地构建/设计此数据库以构建 Slack 克隆的建议。

最佳答案

如果要在客户端上运行更新操作,则需要使用_id字段。否则你会得到这个错误:

Error: Not permitted. Untrusted code may only update documents by ID. [403]

因此,首先获取文档,然后使用文档的 _id 运行更新查询。

例如:

var conversation = Conversations.findOne({
"channel": "#defaultChannel"
});

Conversations.update({
_id: conversation._id
}, {
$push: {
entries: {
message: "newMessage",
postedTime: new Date(),
author: "coffeemeup"
}
}
});

更新后的对话文档如下所示:

{
"_id": "wGixGJgoM6fk57mtN",
"channel": "#defaultChannel",
"createdBy": "coffeemeup",
"timestamp": "2015-07-27T19:25:52.842Z",
"followers": [
"username1",
"username2"
],
"entries": [
{
"message": "this is a message #1",
"postedTime": "2015-07-27T19:25:52.842Z",
"author": "coffeemeup"
},
{
"message": "newMessage",
"postedTime": "2015-07-27T19:27:54.930Z",
"author": "coffeemeup"
}
]
}

关于javascript - 无法将新数组 $push 到 Meteor Collection 中的文档中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31597515/

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