解析服务器云代码保存对象

时间:2016-04-29 19:11:21

标签: javascript parse-platform cloud-code

我正在尝试查询对象添加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 个答案:

答案 0 :(得分:3)

我的问题是语法相关,解析服务器云代码的语法严重缺乏,因为它与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.');

        }
      });
    }
  });

});




答案 1 :(得分: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
  )
});