Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用meteor.js更新嵌套json数组_Javascript_Meteor - Fatal编程技术网

Javascript 如何使用meteor.js更新嵌套json数组

Javascript 如何使用meteor.js更新嵌套json数组,javascript,meteor,Javascript,Meteor,我想将这些值插入mongodb,这是我的代码,我可以插入它,但当我在admin_部分中更新only access_策略时,我无法这样做,请任何人帮助我,我如何解决它: meteor.json { "_id" : ObjectId("555ebffa7c88507a4a683e81"), "section_id" : "Admin Section", "is_active" : true, "admin_section" : [ {

我想将这些值插入mongodb,这是我的代码,我可以插入它,但当我在admin_部分中更新only access_策略时,我无法这样做,请任何人帮助我,我如何解决它: 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.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" } 
    }
  );
{
var doc=MyCollection.findOne(this.\u id);
var adminSections=doc.admin\u节;
对于(i=0;i
它仅适用于“admin\u section”的第一个数组,但我必须根据数组的Id将每个数组的“access\u policy”更新为动态,请提供帮助。请参阅更新的答案-这就是您要查找的吗?
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" } 
    }
  );
{