如何使用Parse cloud js保存我的模型

How to save my model using Parse cloud js?

本文关键字:保存 我的 模型 js cloud 何使用 Parse      更新时间:2023-09-26

我读了一个模因的例子,但它似乎没有更新,只是创建新的对象!我想要的是

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");
    }
);

我认为上面的代码会工作-但它没有。当它找到一个对象,它反而拒绝保存,愚蠢地告诉我,我的对象没有"保存"方法。

首先,我会仔细检查您在云代码中使用的javascript sdk的版本。确保它是最新的,例如1.2.8。版本号在config/global文件中设置。

假设你是最新的,我会尝试修改你的代码通过链接承诺使用多个then,像这样:

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.
        });

认为这应该可以工作。祈祷。干杯!