[英]How to update existing values and save new values in parse.com
我在我的項目中使用parse應用程序,我看到了parse中的文檔,並提到它們分別用於保存和更新。 我想做sql,如果表中存在該行,則應更新值,否則應重新保存值。 我如何在單一功能中做到這一點。 這是我的代碼
Parse.initialize(parseId.APPLICAITONID, parseId.JAVASCRIPTID);
var locationMobileMsgs = Parse.Object.extend("LocationMobileMessages");
var locationMsgs = new Parse.Query(locationMobileMsgs);
locationMsgs.equalTo('LocationId',123);
locationMsgs.find({success:function(data){
// if the values are not there i need to use query.save here.
// otherwise i can do below
data.set('BodyText',msgObj.mobileTextarea);
data.set('HeaderText',msgObj.mobileHeadline);
data.set('LocationId',msgObj.locationId);
data.save();
console.log('saved');
},error:function(errData){
console.log('error occured');
res.json(errData);
}
});
我認為您可以簡化使用if
語句:
locationMsgs.find({success: function (results) {
// if the values are not there i need to use query.save here.
if (results.length === 0) {
//insert...
} else {
// otherwise i can do below
results.forEach(function (data) {
data.set('BodyText', msgObj.mobileTextarea);
data.set('HeaderText', msgObj.mobileHeadline);
data.set('LocationId', msgObj.locationId);
data.save();
});
}, error: //...
或者,如果您最多有一個要更新的對象:
locationMsgs.first({success: function (data) {
// if the values are not there i need to use query.save here.
if (!data) {
//insert...
} else {
// otherwise i can do below
data.set('BodyText', msgObj.mobileTextarea);
data.set('HeaderText', msgObj.mobileHeadline);
data.set('LocationId', msgObj.locationId);
data.save();
}, error: //...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.