Cloud Functions for Firebase 突然超时
Cloud Functions for Firebase suddenly timing out
今天部署完我的功能后突然发现我们的一些功能总是超时。我在云功能控制台中看到了这一点。这似乎发生在嵌套和返回承诺时。奇怪的是,这从来没有发生过。
这是我的代码。
exports.pushMessageQueue = functions.database.ref('/userPushMessagesQueue/{userId}/{messageId}').onWrite(event => {
if (!event.data.exists()) {
console.log('DOES NOT EXIST!');
return "not exists";
}
console.log("EXISTS!");
const userId = event.params['userId'];
const messageId = event.params['messageId'];
const payload = event.data.val();
return database.ref('devices').orderByChild('user_id').equalTo(userId).once('value', snapshot => {
if (!snapshot.exists()) {
return "no devices found";
}
const devices = snapshot.val();
const deviceTokens = [];
snapshot.forEach(deviceSnap => {
const device = deviceSnap.val();
deviceTokens.push(device['fcm_key']);
});
const removeFromQueue = database.ref(`/userPushMessagesQueue/${userId}/${messageId}`);
console.log('then0');
return removeFromQueue.set(null).then(() => {
console.log('then1');
return admin.messaging().sendToDevice(deviceTokens, payload).then(() => {
console.log('then2');
return "send";
});
}, (error) => {
console.log('error1!');
console.log(error);
});
});
});
在控制台中记录了 then0,而不是 then1 和 then2。当我收到推送通知并且条目从队列中删除时。
我做错了什么吗?
您返回的是 once() 函数,而不是承诺链。
基本上,你写了这个:
//returns the once promise
return ref.once('value', function(snap) {
/* some stuff that doesn't affect the promise chain happens here */
});
当你想要的是:
// returns whatever happens in the .then() callback
return ref.once('value').then(snap => {
/* now whatever you return here affects the promise chain */
});
今天部署完我的功能后突然发现我们的一些功能总是超时。我在云功能控制台中看到了这一点。这似乎发生在嵌套和返回承诺时。奇怪的是,这从来没有发生过。
这是我的代码。
exports.pushMessageQueue = functions.database.ref('/userPushMessagesQueue/{userId}/{messageId}').onWrite(event => {
if (!event.data.exists()) {
console.log('DOES NOT EXIST!');
return "not exists";
}
console.log("EXISTS!");
const userId = event.params['userId'];
const messageId = event.params['messageId'];
const payload = event.data.val();
return database.ref('devices').orderByChild('user_id').equalTo(userId).once('value', snapshot => {
if (!snapshot.exists()) {
return "no devices found";
}
const devices = snapshot.val();
const deviceTokens = [];
snapshot.forEach(deviceSnap => {
const device = deviceSnap.val();
deviceTokens.push(device['fcm_key']);
});
const removeFromQueue = database.ref(`/userPushMessagesQueue/${userId}/${messageId}`);
console.log('then0');
return removeFromQueue.set(null).then(() => {
console.log('then1');
return admin.messaging().sendToDevice(deviceTokens, payload).then(() => {
console.log('then2');
return "send";
});
}, (error) => {
console.log('error1!');
console.log(error);
});
});
});
在控制台中记录了 then0,而不是 then1 和 then2。当我收到推送通知并且条目从队列中删除时。
我做错了什么吗?
您返回的是 once() 函数,而不是承诺链。
基本上,你写了这个:
//returns the once promise
return ref.once('value', function(snap) {
/* some stuff that doesn't affect the promise chain happens here */
});
当你想要的是:
// returns whatever happens in the .then() callback
return ref.once('value').then(snap => {
/* now whatever you return here affects the promise chain */
});