2

Ok, so I am trying to use the afterSave function to change some values in a separate table. In my setup, the object that changes in the first table has knowledge of a unique field in the second. I feel like the process should go: afterSave -> call get of specific column -> use that to query the second table -> set a field of the found object. When I try this, nothing changes. I'd really appreciate any help. Thank you!

Parse.Cloud.afterSave("A", function(request){
    var A_obj = request.object
    if (A_obj.get("TriggerColumn") == "TriggerValue") {
        var B = Parse.Object.extend("B")
        var query = new Parse.Query(B)
        query.equalTo("B_Data", A_obj.get("Known_B_Data"))
        query.find({
            success: function(b_array) {
                var b = b_array[0]
                b.set("B_Other_Data","New Data")
                b.save()
            }
        })
    }
})
kRiZ
  • 2,320
  • 4
  • 28
  • 39
Josh Carter
  • 111
  • 1
  • 9

2 Answers2

0

You will need to query for you A_obj to get it's values. Your if condition must always be returning false because of this.

Query for your A_obj first:

Parse.Cloud.afterSave("A", function(request){
    var A_obj_query = new Parse.Query("A");
    A_obj_query.get(request.object.id, {
        success: function(A_obj) {
            if (A_obj.get("TriggerColumn") == "TriggerValue") {
                // ...
            }
        },
        error: function(error) {
            // ...
        }
    });
});
kRiZ
  • 2,320
  • 4
  • 28
  • 39
  • no need to query the object from the afterSave of object A. Just get back information with `request.object.get(FIELD_Requested)` – Toucouleur Jun 30 '15 at 03:57
0

better write your code like this :

Parse.Cloud.afterSave("A", function(request){
    if (request.object.get("TriggerColumn") == "TriggerValue") {
        var B = Parse.Object.extend("B");
        var query = new Parse.Query(B);
        query.equalTo("B_Data", request.object.get("Known_B_Data"));
        return query.find().then(function(results_B) {
            Parse.Promise.when(results_B.map(function(b) {
                b.set("B_Other_Data","New Data");
                b.save();
            }));
        });
    }
})

I think your only mistake was here : var b = b_array[0]

Toucouleur
  • 1,194
  • 1
  • 10
  • 30