gpt4 book ai didi

javascript - 错误 : function crashed out of request scope Function invocation was interrupted. 使用大列表

转载 作者:行者123 更新时间:2023-12-02 22:38:24 24 4
gpt4 key购买 nike

我正在尝试执行向列表中的所有用户发送推送通知的云功能。当列表很小时,这很有效。我认为问题是由于我发送推送的调用数量造成的。有什么想法吗?

云功能:

exports.sendPushToUsers = functions.https.onCall((data, response) => {
var promisesSendPushs = [];
return admin.database().ref(Constants.USERS).once("value")
.then((usersSnap) => {
if(usersSnap.exists()) {
usersSnap.forEach(userSnap => {
if(userSnap.exists()) {
var user = userSnap.val();
if(user.firebaseToken !== undefined) {
var promiseSendPush;
if(user.platform === Constants.PLATFORM.IOS) {
promiseSendPush = pushs.customIOS.customized(user.firebaseToken, user.uid, data.title, data.body);
} else {
promiseSendPush = pushs.customAndroid.customized(user.firebaseToken, user.uid, data.title, data.body);
}
promisesSendPushs.push(promiseSendPush);
}
}
});
return Promise.all(promisesSendPushs)
} else {
console.log("usersSnap undefined");
return {code: 0, state: 'undefined'};
}
})
.then(() => {
console.log("promisesSendPushs: " + promisesSendPushs.length)
return {code: 0, state: 'pushs send!'};
})
.catch(err => {
console.log ('Error getting documents', err);
return { code: 1, error: err};
});
})

自定义推送库:

const admin = require('firebase-admin');

function sendPushToUser(firebaseToken, message, uid) {
if(firebaseToken) {
return admin.messaging().sendToDevice(
[ firebaseToken ],
message
).then(() => {
return { code: 0}
})
.catch((err) => {
return { code: 1}
});
} else {
return { code: 1 }
}
}

exports.customized = function(firebaseToken, uid, title, body) {
let message = {
data: {
title: `${title}`,
body: `${body}`
}
};
return sendPushToUser(firebaseToken, message, uid);
};

控制台 firebase 错误:

Error: function crashed out of request scope

函数调用被中断。

最佳答案

您可能不想将所有这些 promise 的结果发送给客户端:

return Promise.all(promisesSendPushs)

云函数在尝试序列化所有已解决的 Promise 的所有结果时可能会崩溃,这可能会变得非常大。

相反,请找出在所有 promise 都成功的情况下要发送给客户端的响应。例如:

return Promise.all(promisesSendPushs)
.then(results => {
return { the object to send to the client on success }
})
.catch(error => {
return { the object to send to the client on error }
})

关于javascript - 错误 : function crashed out of request scope Function invocation was interrupted. 使用大列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58666738/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com