[英]Parse Cloud Code - Add To Array
Tried loads of different variations with my cloud code and I can't get it to work.用我的云代码尝试了大量不同的变体,但我无法让它工作。 Basically I've got a push notification function, and in this function I want to add an object to a PFUser's array, but you can't use a master key in Xcode so here's what I have:
基本上我有一个推送通知功能,在这个功能中,我想将一个对象添加到 PFUser 的数组中,但是你不能在 Xcode 中使用主密钥,所以这就是我所拥有的:
Parse.Cloud.define("iOSPush", function (request, response) {
console.log("Inside iOSPush");
var data = request.params.data;
var not_class = request.params.not_class;
var not_objectid = request.params.not_objectid;
var not_date = request.params.not_date;
var userid = request.params.userid;
var recipientUser = new Parse.Query(Parse.User);
recipientUser.equalTo("objectId", userid);
// set installation query:
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.equalTo('deviceType', 'ios');
pushQuery.matchesQuery('user', recipientUser);
pushQuery.find({ useMasterKey: true }).then(function(object) {
response.success(object);
console.log("pushQuery got " + object.length);
}, function(error) {
response.error(error);
console.error("pushQuery find failed. error = " + error.message);
});
// send push notification query:
Parse.Push.send({
where: pushQuery,
data: data
}, { useMasterKey: true }).then(function() {
console.log("### push sent!");
// create notification:
var notification = {
"title": not_class,
"body": request.params.data.alert,
"class": not_class,
"objectId": not_objectid,
"date": not_date
};
// get notifications:
var tmp_notifications = recipientUser.get("notifications");
// add notification:
tmp_notifications.push(notification);
// update with notifications:
recipientUser.set("notifications", tmp_notifications);
recipientUser.save();
}, function(error) {
console.error("### push error" + error.message);
});
response.success('success. end of iospush');
});
The Xcode cloud function I have provides the correct information, the function gets to the end.. just the function is not setting the notifications for some reason我提供的 Xcode 云功能提供了正确的信息,该功能结束了.. 只是该功能由于某种原因没有设置通知
I ended up figuring out the answer to this post myself.我最终自己找出了这篇文章的答案。 The reason this didn't work is because I needed to first fetch the user object in a separate query, then save it using the master key.
这不起作用的原因是因为我需要首先在单独的查询中获取用户对象,然后使用主密钥保存它。 I also found out that there's a function for appending data onto an existing array without having to create another one (parseObject.add()):
我还发现有一个函数可以将数据附加到现有数组上,而无需创建另一个数组 (parseObject.add()):
var userQ = new Parse.Query(Parse.User);
userQ.get(userid, {
success: function(theuser) {
console.log("### got userrrrrrrrrr!");
theuser.add("notifications", n_object);
theuser.save(null, {useMasterKey:true});
},
error: function(object, error) {
// The object was not retrieved successfully.
// error is a Parse.Error with an error code and message.
}
});
This set of code was executed just before:这组代码就在之前执行过:
response.success('success. end of iospush');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.