2015-05-22 19 views
5

Bu değerleri mongodb içine eklemek istiyorum ve burada benim kodum, bunu ekleyebildiğim ama sadece access_policy içinde admin_section güncellenirken, yapamadım o, herkes lütfen bana yardımcı olabilir, nasıl bunu çözebilir: meteor.jsonmeteor.js kullanarak yuvalanmış json dizisini güncelleme

{ 
    "_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}} 
); 
} 

cevap

0

Meteor.call numaralı telefonu aramadan ve sunucu tarafı yönteminin güncelleştirmeyi yapmasına izin vermediğiniz sürece, tüm istemci tarafı kodları güvenilmez olarak kabul edilir. Konsolda bakarsanız, muhtemelen böyle bir hata göreceksiniz: Eğer bir kerede yalnızca 1 dokümanı güncelleyebilir, istemci tarafında

{ 
    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" 
} 

ve belge kimliğine göre güncellemek için seçmelisiniz, böylece 1 argümanı bir Mongo belge kimliği olması gerekir değil, bir Mongo seçici:

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

, yerine bir seçici, haber olarak 1 argüman kimliğini belirtmek yerine bir dizinin bir alt belgeyi güncellemek için kimliğini $set deyiminde nasıl belirtiyorum.

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" } 
    } 
); 
{ 
+0

o "admin_section" ilk dizisi için işe yarar, ama "access_policy" güncellemek zorunda: Eğer dizideki tüm elemanları güncellemek istiyorsanız

ayrı ayrı hepsini döngü var Bir dizi Id göre dinamik olarak her dizi, lütfen yardım edin. –

+0

Güncellenmiş yanıtlara bakın - aradığınız şey bu mu? –

İlgili konular