我读过了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");
}
);
我认为上面的代码可行 - 但事实并非如此。当它找到一个对象时,它反而拒绝保存,愚蠢地告诉我我的对象没有“保存”方法。
答案 0 :(得分:1)
首先,我会仔细检查您在云代码中使用的javascript sdk的版本。确保它是最新的,例如1.2.8。该版本在您的云代码目录下的config / global.json文件中设置。
假设你是最新的,我会尝试通过使用多个来链接promises来修改你的代码,如下所示:
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.
});
我认为这应该有效。手指交叉。干杯!