gpt4 book ai didi

javascript - 如何使用 meteor.js 更新嵌套的 json 数组

转载 作者:搜寻专家 更新时间:2023-11-01 04:41:02 27 4
gpt4 key购买 nike

我想将这些值插入到 mongodb 中,这是我的代码,我可以插入它但是当我只更新 admin_section 中的 access_policy 时,我无法做到这一点,任何人都可以帮助我,怎么能我解决了: meteor .json

{
"_id" : ObjectId("555ebffa7c88507a4a683e81"),
"section_id" : "Admin Section",
"is_active" : true,
"admin_section" : [
{
"_id" : 1,
"info" : "Setup custom access policy for user groups",
"sub_section" : "Setup - access policy",
"access_policy" : "ROOT"
},
{
"_id" : 2,
"info" : "Customize access policy for main sections",
"sub_section" : "Manage - access policy",
"access_policy" : "ROOT"
},
{
"_id" : 3,
"info" : "Manage user groups for DX-Client application",
"sub_section" : "Manage - user groups",
"access_policy" : "ROOT"
},
{
"_id" : 4,
"info" : "Manage users for DX-Client application",
"sub_section" : "Create - user",
"access_policy" : "ADMINISTRATOR"
},
],
"access_policy" : "ADMINISTRATOR"
}

meteor .html

  <table class="table table-bordered">
<tbody>
{{#each temp_admin_section}}
{{#if admin_section}}
<tr class="bgLight">
<td class="left" id="one"><b>{{ section_id }}</b></td>
<td class="left" id="two"><b>RESTRICTED</b> - <code contenteditable="true" class="edited"><input value="{{ access_policy }}" /></code><a class="add-access pull-right">add</a></td>
</tr>
{{#each admin_section}}
<tr>
<td class="left" id="one">{{ sub_section }}</td>
<td class="left" id="two">RESTRICTED - <code contenteditable="true" class="edited">
<input value="{{ access_policy }}" /></code></td>
</tr>
{{/each}}
{{/if}}
{{/each}}
</tbody>
</table>

meteor .js

 'change .edited': function(event, template){
var to = event.target.value;
var list = to.split(",");
map.update(
{"_id": this._id, "admin_section._id" : 1},
{$set: {"admin_section.$.access_policy": list}}
);
}

最佳答案

除非您调用 Meteor.call,否则所有客户端代码都被认为是不受信任的并让服务器端方法进行更新。如果您查看控制台,您可能会看到如下错误:

 {
error: 403,
reason: "Not permitted. Untrusted code may only update documents by ID.",
details: undefined, message: "Not permitted. Untrusted code may only update documents by ID. [403]",
errorType: "Meteor.Error"
}

在客户端,您一次只能更新 1 个文档,并且必须通过 ID 选择要更新的文档,因此第一个参数需要是 mongo 文档 ID,而不是 mongo 选择器:

MyCollection.update(
this._id,
{
$set: { "admin_section.1.access_policy": "TEST" }
}
)

此外,要更新数组的子文档,而不是在第一个参数中将 ID 指定为选择器,请注意我是如何在 $set 中指定 ID 的声明。

如果你想更新数组中的所有元素,你必须单独遍历所有元素:

var doc = MyCollection.findOne(this._id);
var adminSections = doc.admin_section;

for (i = 0; i < adminSections.length; i++) {
// If you need to access a value in the nested doc:
var nestedDoc = adminSections[i];
MyCollection.update(
this._id,
{
$set: { "admin_section." + (i+1) + ".access_policy": "someValue" }
}
);
{

关于javascript - 如何使用 meteor.js 更新嵌套的 json 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30397875/

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