简体   繁体   中英

parse cloud code save relation

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.

Cleaned up the code a little, and added success() / error() responses, which, I believe, is why it wasn't working.

Parse.Cloud.afterSave("Notification", function(request, response) {

    var tytype = request.object.get('type');
    if (tytype !== 70) return response.success();

    var alice = Parse.User.current();
    var alicesRelation = alice.relation("potFriendsList");

    // EDIT - query user this way...
    var bobQuery = new Parse.Query(Parse.User);
    bobQuery.equalTo("username", request.object.get('nameB'));

    bobQuery.first().then(function(bob) {
        var bobsRelation = bob.relation("potFriendsList");

        alicesRelation.add(bob);
        bobsRelation.add(alice);

        return Parse.Object.saveAll([alice, bob]);

    }).then(function() {
        console.log("success " + arguments);
        response.success(arguments);
    }, function(error) {
        console.log("error " + error.message);
        response.error(error);
    });
});

A few other notes: (0) dispense with the non- tytype==70 cases right away, (1) query.first() will get just the first result so there's no need to limit(1) and iterate, (2) Parse.Object.saveAll() will save an array of objects, and that's useful since you have two.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM