Firebase云函数报错:TypeError: sendEachForMulticast不是函数
问题:Firebase Cloud Function调用sendEachForMulticast报错TypeError
运行Firebase Cloud Function时触发以下错误:
TypeError: admin.messaging(...).sendEachForMulticast is not a function at /workspace/index.js:51:49 at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
完整函数代码
// Import the Firebase SDK for Google Cloud Functions. const functions = require('firebase-functions'); // Import and initialize the Firebase Admin SDK. const admin = require('firebase-admin'); admin.initializeApp(); // Sends notifications to all users when a new message is posted. exports.sendNotifications = functions.firestore.document('CustomersWithoutBooking/{messageId}').onCreate( async (snapshot) => { // Notification details. const text = snapshot.data().text; const payload = { notification: { title: 'TEST', body: 'TESTBODY', } }; // Get the list of device tokens. const allTokensSnapshot = await admin.firestore().collection('Behandlere').get(); const tokens = []; allTokensSnapshot.forEach((tokenDoc) => { tokens.push(tokenDoc.id); }); if (tokens.length > 0) { // Send notifications to all tokens using sendEachForMulticast. const responses = await admin.messaging().sendEachForMulticast({ tokens: tokens, notification: payload.notification, }); // Cleanup invalid tokens after sending notifications await cleanupTokens(responses, tokens); functions.logger.log('Notifications have been sent and tokens cleaned up.'); } } ); // Cleans up the tokens that are no longer valid. async function cleanupTokens(response, tokens) { const tokensDelete = []; response.responses.forEach((result, index) => { const error = result.error; if (error) { functions.logger.error('Failure sending notification to', tokens[index], error); if (error.code === 'messaging/invalid-registration-token' || error.code === 'messaging/registration-token-not-registered') { const deleteTask = admin.firestore().collection('fcmTokens').doc(tokens[index]).delete(); tokensDelete.push(deleteTask); } } }); await Promise.all(tokensDelete); }
package.json配置
{ "name": "friendlychat-codelab", "description": "Firebase SDK for Cloud Functions codelab", "dependencies": { "@google-cloud/vision": "^2.4.0", "firebase-admin": "~9.9.0", "firebase-functions": "^3.14.1" }, "devDependencies": { "eslint": "^7.23.0", "eslint-plugin-promise": "^4.3.1" }, "engines": { "node": "20" }, "private": true }
解决方案
1. 核心原因
sendEachForMulticast是Firebase Admin SDK v10.0.0及以上版本才新增的API,当前使用的firebase-admin@9.9.0版本不支持该方法,导致调用时报错。同时当前指定的Node.js版本为20,而v9.9.0版本的firebase-admin并不支持Node.js 20,存在版本兼容性问题。
2. 修复步骤
- 升级firebase-admin依赖:修改package.json中
firebase-admin的版本为^11.0.0或更高(推荐使用最新稳定版,确保兼容Node.js 20),例如:"firebase-admin": "^11.11.0" - 同步firebase-functions版本:建议将
firebase-functions升级到与新版admin兼容的版本,比如^4.0.0及以上:"firebase-functions": "^4.5.0" - 执行依赖更新:在终端运行
npm install安装新版本依赖。 - 修正函数参数bug:原代码中
cleanupTokens函数的参数名为response,但调用时传入的是responses,会导致后续response.responses访问出错,需将函数定义修正为:async function cleanupTokens(responses, tokens) { const tokensDelete = []; responses.responses.forEach((result, index) => { // 原有逻辑保持不变 }); await Promise.all(tokensDelete); }
内容的提问来源于stack exchange,提问作者BluE_MoOn
相关产品推荐
相关产品推荐

