Parse Query.First Success / Error Callback intermittently not called

时间:2016-04-25 09:00:16

标签: javascript parse-platform callback cloud-code

I'm working on a Parse Cloud Code issue that has been puzzling me for a couple of days.

I have a function that will check if a user has registered for Push notifications and hasn't disabled them afterwards. It takes a response object as input, a field name to check for a PFUser pointer, and a callback.

function shouldSendPushNotification(object, field, callback){
user = object.get(field);
if(!user){
    console.log("Error: No User in object");
    return;
}
console.log('User is:');
console.log(user);

userId = user.id;
console.log('Seeking push status for user: ' +userId);
console.log(object);

userQuery = new Parse.Query(Parse.User);
userQuery.equalTo("objectId", userId);

console.log('User Query:');
console.log(userQuery);
userQuery.first
({
    success: function(userRetrieved)
    {
        console.log('Successfully found user:');
        console.log(userRetrieved);

        if(userRetrieved.get("pushEnabled") == false){
            console.log('Error: Push not enabled for user. Aborting.');
            callback(false);
            return;
        }

        console.log("Push enabled for user. Continuing.")
        callback(true);
        return;
    },
    error: function(error)
    {
        console.log('Error: Failed to find user for push check.');
        callback(false);
        return;
    }
});
}

I call this method from two locations. In a afterSave function for a "Messages" class which has a pointer reference to the PFUser (user field), and also a custom cloud function which loops through a "Reminder" Class which also has a pointer called "Owner" to PFUser.

The log output of the afterSave method is below (reverse chronological order):

I2016-04-25T08:09:07.716Z] - New object created with objectId: 3olbwjbMVW
I2016-04-25T08:09:07.679Z] - Successfully sent new message push notification to channel: user_7le4EGeKnC with message: New message recieved from Richi Rich
I2016-04-25T08:09:07.578Z] - Fetching Clinic Details for id: 7KgcJMLheu
I2016-04-25T08:09:07.577Z] - Attempting Push to channel: user_7le4EGeKnC
I2016-04-25T08:09:07.576Z] - Push enabled for user. Continuing.
I2016-04-25T08:09:07.575Z] - {"address1":"123 Sesame Street","address2":"","addressPostcode":1234,"addressState":"VIC","addressSuburb":"Port Melbourne","clinic":{"__type":"Pointer","className":"Clinic","objectId":"7KgcJMLheu"},"createdAt":"2016-03-20T09:35:52.252Z","email":"email@email.com","emailVerified":false,"firstName":"Steve","lastName":"Forbes","phone":"0400000000","pushEnabled":true,"updatedAt":"2016-04-18T00:47:54.340Z","username":"email@email.com","objectId":"7le4EGeKnC"}
I2016-04-25T08:09:07.574Z] - Successfully found user:

I2016-04-25T08:09:07.526Z] - {"where":{"objectId":"7le4EGeKnC"}}
I2016-04-25T08:09:07.525Z] - User Query:

I2016-04-25T08:09:07.524Z] - {"clinic":{"__type":"Pointer","className":"Clinic","objectId":"7KgcJMLheu"},"content":"<html><body><p style=\"font-family: serif; font-size: 26px;\">This is a test message from Parse</p></body></html>","createdAt":"2016-04-25T08:09:07.518Z","isUnread":true,"title":"Yo - Test","updatedAt":"2016-04-25T08:09:07.518Z","user":{"__type":"Pointer","className":"_User","objectId":"7le4EGeKnC"},"objectId":"3olbwjbMVW"}

I2016-04-25T08:09:07.523Z] - Seeking push status for user: 7le4EGeKnC
I2016-04-25T08:09:07.522Z] - {"objectId":"7le4EGeKnC"}
I2016-04-25T08:09:07.521Z] - User is:

For the Cloud Function:

I2016-04-25T08:09:53.838Z] - {"where":{"objectId":"7le4EGeKnC"}}
I2016-04-25T08:09:53.837Z] - User Query:

I2016-04-25T08:09:53.836Z] - {"Owner":{"__type":"Pointer","className":"_User","objectId":"7le4EGeKnC"},"createdAt":"2016-04-10T05:45:26.552Z","frequencyInterval":0,"name":"Test","nextReminderDate":{"__type":"Date","iso":"2016-04-26T06:45:00.000Z"},"pet":"Pizza","startDate":{"__type":"Date","iso":"2016-04-12T06:45:14.000Z"},"type":0,"updatedAt":"2016-04-25T07:06:46.986Z","objectId":"BGvPwnq5SB"}

I2016-04-25T08:09:53.835Z] - Seeking push status for user: 7le4EGeKnC
I2016-04-25T08:09:53.834Z] - {"objectId":"7le4EGeKnC"}
I2016-04-25T08:09:53.833Z] - User is:

As you can see the first call executes perfectly - however the second has identical input but the success/error callbacks on the userQuery.First() never get executed. I don't know why!

1 个答案:

答案 0 :(得分:0)

如果要包含所引用的所有函数,将会很有帮助。就目前而言,确定“大图片”确实有点难度。

我的猜测是你的云代码功能没有正确地链接承诺。我建议调整你的函数来返回一个promise,而不是使用回调函数。

可能发生的是Cloud Code函数立即返回而不是等待此函数完成,因为它是异步的。正确使用Parse.Promise将解决此问题。

相关问题