2016-11-24 17 views
0

我正在開發使用JSONStore和AngularJS的Cordova應用程序(使用MobileFirst Foundation 8.0)。無法替換JSONStore集合中的文檔

以下函數更新已存儲在本地集合「議程」中的文檔。

update : function(element){ 

var defer = $.Deferred(); 

// set query for get document stored in local collection 
var query = {id: element.id}; 

// search for exact value 
var options = { 
    exact: true, 
    limit: 1 
}; 

var collection = WL.JSONStore.get('agenda'); 

// invoke search function 
collection.find(query, options)//WL.JSONStore.get('agenda').find(query, options) 
.then(function (results) { 
    // handle success - results (array of documents found) 

    var rowstatus = results[0].json.rowstatus || ''; 

    // substitute current saved document with the update document 
    results[0].json = element; 

    // set value of rowstatus flag 
    results[0].json.rowstatus = rowstatus===''?'UPD':rowstatus; 

    var arrayMod = []; 
    arrayMod.push(results[0]); 

    //invoke replace procedure for update document in local collection 
    collection.replace(arrayMod, {markDirty: false })//WL.JSONStore.get('agenda').replace(item, {markDirty: false}) 
     .then(function(docsRefreshed){ 

      console.log("docsRefreshed", docsRefreshed); 

      defer.resolve(); 
     }) 
     .fail(function(refreshErr){ 

      defer.reject(refreshErr); 
     }); 

}) 
.fail(function (findError) { 

    defer.reject(findError); 
}); 

return defer.promise(); 

} 

的步驟是:

  1. 通過元素的ID找到本地收藏中的文檔 - 通過更換載到 'JSON' 屬性的對象IT WORKS

  2. 更新的文件內容:ID不會改變,我驗證它 - 它工作

  3. 替換集合內的文檔 - IT FAILS

錯誤返回是不是失敗的處理程序,標準誤差攔截,它是這樣的:

VM404 worklight.js:5139 Uncaught Exception: Uncaught RangeError: Maximum call stack size exceeded at (compiled_code):57__log @ VM404 worklight.js:5139PUBLIC_API.(anonymous function) @ VM404 worklight.js:5539WL.Logger.window.onerror @ VM404 worklight.js:5497 VM404 worklight.js:57 Uncaught RangeError: Maximum call stack size exceeded(…)it @ VM404 worklight.js:57__handleJsonObj @ VM416 jsonstore.js:1951__handleJsonObj @ VM416 jsonstore.js:1947__handleJsonObj @ VM416 jsonstore.js:1947__handleJsonObj @ VM416 jsonstore.js:1947__handleJsonObj @ VM416 jsonstore.js:1947__handleJsonObj @ VM416 jsonstore.js:1942__handleJsonObj @ VM416 jsonstore.js:1947__handleJsonObj @ VM416 jsonstore.js:1942__handleJsonObj @ VM416 jsonstore.js:1947__handleJsonObj @ VM416 jsonstore.js:1942__handleJsonObj @ VM416 jsonstore.js:1947__handleJsonObj @ VM416 jsonstore.js:1942__handleJsonObj @ VM416 jsonstore.js:1947__handleJsonObj @ VM416 jsonstore.js:1942__handleJsonObj @ VM416 jsonstore.js:1947__handleJsonObj @ VM416 jsonstore.js:1942__handleJsonObj @ VM416 jsonstore.js:1947__handleJsonObj @ VM416 jsonstore.js:1942__handleJsonObj @ VM416 jsonstore.js:1947__handleJsonObj @ VM416 jsonstore.js:1942__handleJsonObj ....

enter image description here

+0

這個錯誤通常意味着您一遍又一遍地調用一個函數,然後打開瀏覽器內存上限。你如何使用這個功能?有一些很好的stackoverflow文章討論了這個問題,當將onClick處理程序附加到很多事件以及其他一些實例時,這個問題就出現在遞歸中。您的實際更新代碼看起來正確,所以我認爲我們需要查看更多代碼。 – MBillau

+0

@ enrico.visentini我嘗試了從這裏的https://mobilefirstplatform.ibmcloud.com/tutorials/en/foundation/8.0/application-development/jsonstore/cordova/的json商店示例相同的方案,它工作正常,沒有任何錯誤。 –

+0

我試圖修改文檔的'json'元素的一個屬性,它的工作原理。這意味着我無法用更新的('results [0] .json = element;')替換整個'json'對象,因爲它會導致此錯誤。但爲什麼? –

回答

0

從評論通過@ encrico.visentini:

I resolve the issue. The error was caused by a property added automatically from the plugin that is used that causes the circular reference of the json object. Removing this attribute I can replace all the 'json' object.