2016-04-29 128 views
0

我試圖查詢一個對象添加1到返回的整數,然後使用解析服務器雲代碼將此對象保存回我的mLabs數據庫。解析服務器雲代碼保存對象

我可以成功地查詢並將1添加到我想要的對象,但我無法弄清楚如何成功地將其保存回數據庫。我已經嘗試了許多解決方案,所有這些都導致一個解析服務器「請求超時」

Parse.Cloud.define("addRating", function(request, response) { 
 

 
    var currentRatingQuery = new Parse.Query("StudentNotes"); 
 
    currentRatingQuery.equalTo("objectId", "Y4bBzvsHb1"); 
 
    currentRatingQuery.select("noteRating"); 
 
    currentRatingQuery.find({ 
 
    useMasterKey: true, 
 
    success: function(results) { 
 
     //var noteRating = results.get("noteRating"); 
 
     //noteRating += 1; 
 
     results = Number(results); 
 
     results += 1; 
 
     console.log("NOTE RATINGGGGG: " + results); 
 
     console.log("TYPE OFFFFFFF: " + typeof results); 
 

 

 
     results.set('institution', "TEST INSTITUTION"); 
 
     results.save(null, { 
 
     useMasterKey: true 
 
     }); 
 
     console.log("SAVE SUCCESS", results); 
 
     response.success("rating updated successfully.", results); 
 

 
    }, 
 
    error: function(error) { 
 
     response.error("failed to add 1 to parse cloud code rating. Error: " + error); //THIS GETS CALLED 
 
    } 
 
    }); 
 

 
});

上面的代碼成功地查詢數據庫,但不保存值回。它導致分析服務器「請求超時」。

回答

2

我的問題是語法相關,因爲它與parse.com雲代碼非常相似,所以解析服務器雲代碼的語法嚴重缺乏。以下是檢索對象並將對象保存回來的工作代碼。

Parse.Cloud.define('addNoteRating', function(request, response) { 
 
    var SaveObject = Parse.Object.extend("StudentNotes"); 
 
    var saveObject = new Parse.Query(SaveObject); 
 
    saveObject.equalTo("objectId", request.params.objectId); 
 
    saveObject.first({ 
 
    useMasterKey: true, 
 
    success: function(Objects) { 
 
     Objects.save(null, { 
 
     useMasterKey: true, 
 
     success: function(object) { 
 
      object.increment("noteRating"); 
 
      object.save(); 
 
      console.log("Cloud Code: User note rating has increased by 1.", object); 
 
      response.success('Cloud Code: User note rating has increased by 1.'); 
 

 
     } 
 
     }); 
 
    } 
 
    }); 
 

 
});

+0

謝謝!經過數小時和數小時的調試,這也幫助了我 –

1

超時可以通過無響應引起。 嘗試在api失敗時添加響應,並查看發生了什麼。

順便說一句,如果你增加成功時有什麼反應,保存完成時響應。

Parse.Cloud.define('addNoteRating', function(request, response) { 
 
    var SaveObject = Parse.Object.extend("StudentNotes"); 
 
    var saveObject = new Parse.Query(SaveObject); 
 
    saveObject.equalTo("objectId", request.params.objectId); 
 
    saveObject.first({ 
 
    useMasterKey: true, 
 
    //this function will get at most one object 
 
    success: function(object) { 
 
     if(object){ 
 
     object.increment("noteRating"); 
 
     object.save(null,{ 
 
      //do not use master key? 
 
      success: function(note){ 
 
      //u should wait the non-blocking call success and finish 
 
      console.log("Cloud Code: User note rating has increased by 1.", object); 
 
      response.success('Cloud Code: User note rating has increased by 1.'); 
 
      }, error: response.error 
 
     }); 
 
     }else{ 
 
     response.error('this student note is not exist'); 
 
      
 
     } 
 
     
 
    }, error: response.error 
 
    }); 
 
});

如果此對象存在,你可以重寫代碼如下

Parse.Cloud.define('addNoteRating', function(request, response) { 
 
    var SaveObject = Parse.Object.extend("StudentNotes"); 
 
    var studentNote = new SaveObject(); 
 
    studentNote.id = request.params.objectId; 
 
    studentNote.increment("noteRating"); 
 
    //student.save(null,{useMasterKey:true}).then(
 
    studentNote.save().then(
 
    function(note){ 
 
     console.log("Cloud Code: User note rating has increased by 1.", object); 
 
     response.success('Cloud Code: User note rating has increased by 1.'); 
 
    }, response.error 
 
) 
 
});