0

I have an Offer object that I send to the server, when this offer is about to be created I need to send a push notification to the user. Offer has a pointer to User an the field is called "to".

How can I fetch an object from a pointer?

Parse.Cloud.beforeSave("Request", function(request, response) {

    var userQuery = new Parse.Query("User")

    userQuery.get(request.object.get("to").id, {
        success: function (user) {
            console.log("user: ", user);
            var installationQuery = new Parse.Query("Installation");
            installationQuery.equalTo("user", user);

            Parse.Push.send({
                where : installationQuery,
                data : {
                    alert : "HEllo"
                },
                success : function() {

                },
                error : function() {
                    console.log("error finding installation: " + error);
                }
            });
        },
        error : function (error) {
            console.log("ERRRRRRRRRR");
        }
    });

    response.success();
});

1 Answer 1

5

To answer your question directly, you can use Parse.Query.get() or Parse.Object.fetch() to retrieve the object.

I'm assuming that the problem you see is that the object saves but the push notification isn't happening. The cause is that you're not waiting for the get() to complete before calling response.success() and returning.

Here's a couple ways to reconcile that:

Your existing code but with response.success() moved up: Parse.Cloud.beforeSave("Request", function(request, response) {

    var userQuery = new Parse.Query("User")

    userQuery.get(request.object.get("to").id, {
        success: function (user) {
            console.log("user: ", user);
            var installationQuery = new Parse.Query("Installation");
            installationQuery.equalTo("user", user);

            Parse.Push.send({
                where : installationQuery,
                data : {
                    alert : "HEllo"
                },
                success : function() {
                    response.success();
                },
                error : function() {
                    console.log("error finding installation: " + error);
                }
            });
        },
        error : function (error) {
            console.log("ERRRRRRRRRR");
        }
    });
});

Simplified with Promises

Parse.Cloud.beforeSave("Request", function(request, response) {
    request.object.get("to").fetch().then(function(user) {
        var installationQuery = new Parse.Query("Installation");
        installationQuery.equalTo("user", user);

        return Parse.Push.send({
            where : installationQuery,
            data : {
                alert : "HEllo"
            }
        });
    }).then(function() {
        response.success();
    }, response.error);
});

Further simplified. If you're not using the data within user, you shouldn't need to fetch it just to pass a pointer to a query.

Parse.Cloud.beforeSave("Request", function(request, response) {
    var installationQuery = new Parse.Query("Installation");
    installationQuery.equalTo("user", request.object.get("to"));

    return Parse.Push.send({
        where : installationQuery,
        data : {
            alert : "HEllo"
        }
    }).then(function() {
        response.success();
    }, response.error);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the great answer, I believe I tried the last portion of code already, and it didn't work, I think it's because the query result was empty. Will try it again when I get home, and will post a response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.