2014-09-04 53 views
2

這是非常簡單的實現,但可能有一些問題,我的語法在這裏:流星:更新多個配置文件值

Template.userProfilePage.events({ 
    'submit #profile-page': function(event, template) { 
     event.preventDefault(); 
     var name = template.find('#fullName').value, 
     address = template.find('#address').value, 
     company = template.find('#company').value, 
     otherField = template.find('#other').value; 

     alert(name); 

     Meteor.users.update(
      { _id:Meteor.user()._id }, 
      { 
       $set: { 
        "profile.name":name, 
        "profile.address":address, 
        "profile.company":company, 
        "profile.other":other 
       } 
      }, 
      { upsert: true }, 
      { multi: true } 
     ); 

     return false; 
    } 
}); 

模板包含普通的HTML頁面。它始終引發錯誤:

RangeError: Maximum call stack size exceeded.

回答

1

如果您只更新一個用戶,則不需要multi: true。應該也永遠不需要加註;如果您使用的是登錄用戶,則在users集合中應始終有一個文檔。嘗試是這樣的:

Meteor.users.update(
    Meteor.userId(), 
    {$set: { 
    "profile.name": name, 
    "profile.address": address, 
    "profile.company": company, 
    "profile.other": other 
    } 
    } 
); 

另外,還要確保您的allowdeny規則允許你這樣做更新。

P.S.我懷疑你的錯誤信息可能是因爲你有{ multi: true }作爲.update的第四個參數。根據docs,語法是collection.update(selector, modifier, [options], [callback]);因此如果您想同時使用multiupsert,請將它們組合成第三個參數中的一個對象:{ multi: true, upsert: true }(您也可以僅使用collection.upsert而不是.update)。您的錯誤可能是由於您發送了一個對象{ multi: true }作爲第四個參數,而不是update預期的回調函數。

+0

仍然收到相同的錯誤。我將如何檢查「允許」和「拒絕」? – 2014-09-04 19:15:11

+0

還有一件事,很少的值可能是'insert'而不是'update'。這就是爲什麼我在我的代碼中有'{懊惱:真'}。 – 2014-09-04 19:16:50

+2

Upsert僅適用於整個文檔,而不適用於單個字段;請參閱http://docs.meteor.com/#upsert。你確定'name','address'等都是有效的嗎?你是否像我在我的文章中描述的那樣解決了這些爭論? – 2014-09-04 19:22:52