I am developing an app with friends relations using Parse as a backend. When Alice sends a friend request to Bob she generates a notification of type 70 with herself as userA and Bob as userB. For convenience the notification also has nameA and nameB as strings.
I am trying to implement an AfterSave function on Cloud Code. This function should add Bob to Alice's "potential friends list" and Alice to Bob's "potential friends list. For some reasons I can only add Bob to Alice's list, but not the other.
Here is my code:
Parse.Cloud.afterSave("Notification", function(request, response) {
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 === 70) {
var bobQuery = new Parse.Query("User");
bobQuery.equalTo("username", namnamB);
bobQuery.limit(1);
bobQuery.find().then(function(bobs){
bobs.forEach(function(bobby) {
var bobbysRelation = bobby.relation("potFriendsList");
var alicesRelation = alice.relation("potFriendsList");
alicesRelation.add(bobby);
alice.save().then(function(obj){
console.log("alice has a new potential friend" + obj);
bobbysRelation.add(alice);
bobby.save().then(function(obj){
console.log("bobby has a new potential friend" + obj);
},
function(obj, error){
console.log(error);
});
},
function(obj, error){
console.log(error);
});
});
});
}
});
I am new to JS and I haven't been able to make this work for hours. Thank you very much for any help.