2013-06-19 19 views
0

我讀了一個meme的例子,但它似乎沒有更新,只是創建新的對象!我想要的是如何使用Parse雲js保存我的模型?

a. find some given db table 
b. update some fields in the db table 
c. save the db table back to the database 

鑑於此代碼,什麼是缺少一塊,以便我可以實際更新對象?

query.find( 
    function(results){ 
     if (results.length > 0){ 
      return results[0]; 
     } else { 
      //no object found, so i want to make an object... do i do that here? 
      return null; 
     } 
    }, 
    function(error){ 
     response.error("ServerDown"); 
     console.error("ServerDown - getModuleIfAny URGENT. Failed to retrieve from the ModuleResults table" + +error.code+ " " +error.message); 
    } 
).then(
    function(obj){ 
     var module; 
     if (obj != null){ 
      console.log("old"); 
      module = obj; 
      module.moduleId = 10; //let's just say this is where i update the field 
      //is this how i'd update some column in the database? 
     } else { 
      console.log("new"); 
      var theModuleClass = Parse.Object.extend("ModuleResults"); 
      module= new theModuleClass(); 
     } 

     module.save().then(
      function(){ 
       response.success("YAY"); 
      }, 
      function(error) { 
       response.error('Failed saving: '+error.code); 
      } 
     ); 
    }, 
    function(error){ 
     console.log("sod"); 
    } 
); 

我認爲上面的代碼可以工作 - 但事實並非如此。當它發現一個對象時,它拒絕保存,愚蠢地告訴我,我的對象沒有「保存」方法。

回答

1

首先,我會仔細檢查您的雲代碼中使用的javascript sdk的版本。確保它是最新的,例如1.2.8。該版本在您的雲代碼目錄下的config/global.json文件中設置。

假設你是迄今爲止我會嘗試通過使用多個然後像這樣鏈接的承諾修改代碼:

query.find().then(function(results){ 
         if (results.length > 0){ 
          return results[0]; 
         } else { 
          //no object found, so i want to make an object... do i do that here? 
          return null; 
         } 
        }, 
        function(error){ 
         response.error("ServerDown"); 
         console.error("ServerDown - getModuleIfAny URGENT. Failed to retrieve from   the ModuleResults table" + +error.code+ " " +error.message); 
      }).then(function(obj){ 
    var module; 
    if (obj != null){ 
     console.log("old"); 
     module = obj; 
     module.moduleId = 10; //let's just say this is where i update the field 
     //is this how i'd update some column in the database? 
    } else { 
     console.log("new"); 
     var theModuleClass = Parse.Object.extend("ModuleResults"); 
     module= new theModuleClass(); 
    } 

    module.save(); 
}).then(function(result) { 
      // the object was saved. 
     }, 
     function(error) { 
      // there was some error. 
     }); 

認爲這應該工作。手指交叉。乾杯!

+0

你是如何獲得最新解析雲SDK的版本? – bharal

相關問題