Code Cloud Code callback not working
I am creating a Friends Relationship app using Parse Cloud Code.
When Alice sends Bob a friend request, this is a type 71 notification. When Bob answers, he sends a type 8 notification.
On the server, when a type 8 notification is sent, some friendship is done first. These are: removing each other's "potential friend list" from both users and adding to the "buddy list" instead.
Subsequently, type 71 notification should be changed to type 1 notification.
For some reason, I struggled for 24 hours to get it to work. I just cannot execute these two functions one after the other: the second is never executed. Here is my code
Parse.Cloud.afterSave("Notification", function(request, response) {
Parse.Cloud.useMasterKey();
var downQuery = new Parse.Query("Notification");
var namnamA = request.object.get('nameA');
var namnamB = request.object.get('nameB');
var tytype = request.object.get('type');
var alice = Parse.User.current();
if (tytype === 8){
var bobQuery = new Parse.Query(Parse.User);
bobQuery.equalTo("username", namnamB);
bobQuery.first().then(function(bob) {
var alicesRelation = alice.relation("friendsList");
var alicesPotRelation = alice.relation("potFriendsList");
var bobsRelation = bob.relation("friendsList");
var bobsPotRelation = bob.relation("potFriendsList");
alicesPotRelation.remove(bob);
alicesRelation.add(bob);
bobsPotRelation.remove(alice);
bobsRelation.add(alice);
return Parse.Object.saveAll([alice, bob]);
}).then(function() {
downQuery.equalTo('nameA', namnamB);
downQuery.equalTo('nameB', namnamA);
downQuery.equalTo('type', 71);
return downQuery.find();
}).then(function(notizz) {
notizz.set('type', 1);
return Parse.Object.saveAll([notizz]);
}).then(function() {
console.log("success " + arguments);
response.success(arguments);
}), function(error) {
console.log("error " + error.message);
response.error(error);
}
}
});
Any help will greatly improve the life expectancy of my computer. Thank.
source to share
aftersave has no answer, only a request. https://parse.com/docs/js/guide
Hence, you don't need (and shouldn't) use the answer.
Next when you call return it exits the function
source to share