如何使用Parse cloud js保存我的模型
How to save my model using Parse cloud js?
我读了一个模因的例子,但它似乎没有更新,只是创建新的对象!我想要的是
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.
});
我认为这应该可以工作。祈祷。干杯!
相关文章:
- 如何使用默认的网络摄像头拍摄照片并将其保存在我的c#.net web应用程序中
- 如何从重定向的支付处理器网站提取交易详细信息并保存到我的rails 4数据库
- 在我的浏览器中用类似ctrl+s的PhantomJS保存一个网站
- 我的函数不会返回要保存在数组中的对象
- 无法将所有位置值保存到我的数组中
- 我的jquery代码在谷歌chrome控制台中工作,而不是在保存它之后
- 我应该把维基百科的数据保存到我的数据库中吗
- 设置超时可以保存我的循环函数不被视为无响应
- 为什么我的保存时间功能不起作用?PHP + AJAX JS.
- 我的数据没有使用 AJAX 保存在数据库中
- 如何保存我的用户名或套接字
- 如何保存我的javascript数组到一个文件
- 如何使用Parse cloud js保存我的模型
- 如何保存我的初始页面's url作为js中的一个变量,并在表单提交后使用它
- 如何不允许人们保存我的网站的源代码
- 如何保存我的自定义宽度到一个cookie
- 如何保存我的小注册页面中的所有数据并显示它们
- 如何使用jquery保存我的表单数据,然后将其加载到fancybox中以预览更改
- 我怎么能添加selected=selected保存我的下拉菜单,如果有一个错误在我的形式
- 为什么我的post请求PostgreSQL DB保存我的UTC DATETIME作为本地时间