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();
});
有帮助吗?

解决方案

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);
});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top