简体   繁体   中英

Let user turn off different notification types using parse.com's cloud code

First time posting on here so bear with me. I am taking a dive into parse.com's cloud code without any knowledge of javascript so I could use a little help.

I got an afterSave push notification working with Cloud Code but I need a way to allow users to subscribe/unsubscribe from different types of notifications. The way I am currently attempting to do this is by storing bool values in the parse user table for the different types of notifications but I'm having trouble getting that value in cloud code. Here is my cloud code:

Parse.Cloud.afterSave("Comment", function(request){
    var comment = request.object;
    var fromUser = request.user;
    var onIdea = comment.get("ideaPointer");
    var ideaOwner = onIdea.get("owner");
    var getNotification = ideaOwner.get("getCommentNotifications");
    var message = fromUser.getUsername() + " commented on your idea.";

    if (getNotification){

        var pushQuery = new Parse.Query(Parse.Installation);
        pushQuery.equalTo("user", ideaOwner);
        Parse.Push.send({
            where: pushQuery,
            data: {
                alert: message,
                ideaId: onIdea.id
            }
        });
    }
});

Here is the error that gets printed to the logs when a comment is saved:

Result: TypeError: Cannot call method 'get' of undefined at main.js:6:34

Here is the line it is having a problem with because it was working before I added it along with the if statement:

var getNotification = ideaOwner.get("getCommentNotifications");

getCommentNotifications is the bool value in the user table.

I'm also not sure if my if statement is written correctly or not:

if (getNotification){}

I have also verified that getCommentNotifications value for the ideaOwner I'm testing on isn't empty.

Any help with this issue or ideas on a better way to allow users to subscribe/unsubscribe from different notifications types would be much appreciated.

The other ends of the those pointers must be fetched. If they really are pointers, then you can treat them as incompletely initialized objects, so...

Parse.Cloud.afterSave("Comment", function(request){
    var comment = request.object;
    var fromUser = request.user;
    var onIdea = comment.get("ideaPointer");
    onIdea.fetch().then(function(onIdeaObject) {
        var ideaOwner = onIdea.get("owner");
        return ideaOwner.fetch();
    }).then(function(ideaOwnerObject) {
        var getNotification = ideaOwnerObject.get("getCommentNotifications");
        if (getNotification) {
            var message = fromUser.getUsername() + " commented on your idea.";
            var pushQuery = new Parse.Query(Parse.Installation);
            pushQuery.equalTo("user", ideaOwnerObject);
            return Parse.Push.send({ where: pushQuery, data: { alert: message, ideaId: onIdea.id } });
        } 
    });
});

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