当我尝试运行以下后台作业时,我收到一个错误,说明Result: TypeError: Object [object Object] has no method 'set'
,指的是我试图将返回对象的"Number"属性设置为值2
的行。我以前用过同样的格式来编辑Parse对象,效果很好,我做错了什么?
Parse.Cloud.job("sendLowPush", function(request, status) {
Parse.Cloud.useMasterKey();
/////////////
//send push notification to all users in the "lowPush" channel
//{Query the # value of pushIncrement with object id hKj2Eazz6h}
var pushIncrement = Parse.Object.extend("pushIncrement");
var pushIncrementQuery = new Parse.Query(pushIncrement);
pushIncrementQuery.equalTo('objectId', 'hKj2Eazz6h');
pushIncrementQuery.find({
success: function(results) {
for (var i = 0; i < results.length; ++i) {
var dayNumber = results[i].get("Number");
}
console.log('dem results be:' + dayNumber);
//Figure out whether its day 1, 2, or 3, and increment. Send lowPush if day 3.
if (dayNumber = 1){
//change it to 2
console.log('dayNumber is 1');
results.set('Number', '2');
results.save();
}
else if (dayNumber = 2){
//change it to 3
console.log('dayNumber is 2');
}
status.success("Push Notifications completed successfully.");
},
error: function(error) {
console.log('shit');
status.error('shitty shit');
}
});
});
results
是对象列表。不能在整个列表上设置Number
字段。然而,看起来您只是在查询具有特定ID的单个对象。在这种情况下,您应该使用.get
:
pushIncrementQuery.get('hKj2Eazz6h', {
success: function(obj) {
var dayNumber = obj.get("Number");
// ...
obj.set('Number', '2');
obj.save();
}
});
如果要使用.find
,则必须访问列表中正确的元素,即results[i]
或results[0]
。