解析 Query.First 间歇性地未调用成功/错误回调
Parse Query.First Success / Error Callback intermittently not called
我正在处理一个困扰我几天的 Parse Cloud Code 问题。
我有一个功能可以检查用户是否注册了推送通知并且之后没有禁用它们。它需要一个响应对象作为输入、一个用于检查 PFUser 指针的字段名称和一个回调。
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;
}
});
}
我从两个位置调用此方法。在 "Messages" class 的 afterSave 函数中,它有一个指向 PFUser(用户字段)的指针引用,还有一个循环通过 "Reminder" Class 的自定义云函数还有一个指向 PFUser 的指针 "Owner"。
afterSave方法的日志输出如下(时间倒序):
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:
对于云函数:
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:
如您所见,第一个调用完美执行 - 但是第二个调用具有相同的输入,但 userQuery.First() 上的 success/error 回调永远不会执行。我不知道为什么!
如果您能包含所有引用的函数,将会很有帮助。就目前而言,有点难以确定 'big picture'.
我猜你的云代码函数没有正确地链接承诺。我建议将您的函数调整为 return 承诺,而不是使用回调函数。
可能发生的情况是 Cloud Code 函数会立即 returning 而不是等待此函数完成,因为它是异步的。正确使用 Parse.Promise 将解决此问题。
我正在处理一个困扰我几天的 Parse Cloud Code 问题。
我有一个功能可以检查用户是否注册了推送通知并且之后没有禁用它们。它需要一个响应对象作为输入、一个用于检查 PFUser 指针的字段名称和一个回调。
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;
}
});
}
我从两个位置调用此方法。在 "Messages" class 的 afterSave 函数中,它有一个指向 PFUser(用户字段)的指针引用,还有一个循环通过 "Reminder" Class 的自定义云函数还有一个指向 PFUser 的指针 "Owner"。
afterSave方法的日志输出如下(时间倒序):
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:
对于云函数:
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:
如您所见,第一个调用完美执行 - 但是第二个调用具有相同的输入,但 userQuery.First() 上的 success/error 回调永远不会执行。我不知道为什么!
如果您能包含所有引用的函数,将会很有帮助。就目前而言,有点难以确定 'big picture'.
我猜你的云代码函数没有正确地链接承诺。我建议将您的函数调整为 return 承诺,而不是使用回调函数。
可能发生的情况是 Cloud Code 函数会立即 returning 而不是等待此函数完成,因为它是异步的。正确使用 Parse.Promise 将解决此问题。