0

我阅读了 meme 示例,但它似乎没有更新,只是创建新对象!我想要的是

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

我认为上面的代码可以工作 - 但它没有。当它找到一个对象时,它拒绝保存,愚蠢地告诉我我的对象没有“保存”方法。

4

1 回答 1

1

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

假设您是最新的,我会尝试通过使用多个 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.
        });

认为这应该有效。手指交叉。干杯!

于 2013-06-19T23:02:26.603 回答