Atlas触发器集成firebase-admin时出现http模块执行错误求助
Atlas触发器中firebase-admin调用sendMulticast时触发@fastify/busboy依赖错误
我在MongoDB Atlas触发器中引入了firebase-admin依赖,昨日代码运行完全正常,但今日调用admin.messaging().sendMulticast(message)时突然抛出以下错误,排查后排除了超时问题(触发器超时设置为90秒,手动运行仍报错)。相同逻辑在Python版本中已稳定运行数月,但Node.js版本出现了HTTP相关的依赖加载错误。
错误信息
FunctionError: 'http' module: FunctionError: failed to execute source for 'node_modules/@fastify/busboy/lib/main.js': FunctionError: failed to execute source for 'node_modules/@fastify/busboy/deps/dicer/lib/Dicer.js': FunctionError: failed to execute source for 'node_modules/@fastify/busboy/deps/dicer/lib/HeaderParser.js': FunctionError: failed to execute source for 'node_modules/@fastify/busboy/lib/utils.js': TypeError: Value is not an object: undefined at node_modules/@fastify/busboy/lib/utils.js:23:27(322) at require (native) at node_modules/@fastify/busboy/deps/dicer/lib/HeaderParser.js:15:24(29) at require (native) at node_modules/@fastify/busboy/deps/dicer/lib/Dicer.js:19:28(39) at require (native) at node_modules/@fastify/busboy/lib/main.js:28:21(44) at execute (native) at <eval>:16:29(8) at K (<eval>:10:6625(35)) at <eval>:10:4454(115) at M (<eval>:10:9199(23)) at T (<eval>:10:9026(174)) at <eval>:10:9810(49) at onStreamRead (<eval>:10:2062(36))
相关代码片段
Firebase初始化代码
const admin = require("firebase-admin"); const json_creds = context.values.get("sdk_key"); admin.initializeApp({ credential: admin.credential.cert(JSON.parse(json_creds)) }); // ... admin.messaging().sendMulticast(message);
消息结构
const message = { android: { notification: { title: title, sound: "default", body: content, }, }, apns: { payload: { aps: { alert: { title: title, body: content, }, mutableContent: 1, contentAvailable: 1 } }, }, tokens: ["token1", "token2"] };
解决方案
- 锁定firebase-admin兼容版本:Atlas触发器的Node.js运行环境对部分新依赖支持有限,建议降级到稳定兼容的版本,比如锁定
firebase-admin@11.11.0(该版本的间接依赖busboy版本更稳定,适配Atlas环境)。修改package.json中的依赖:"dependencies": { "firebase-admin": "11.11.0" } - 强制指定busboy版本:如果不想降级firebase-admin,可以在package.json中添加
overrides字段(npm 8.3+支持),强制使用兼容的@fastify/busboy版本:"overrides": { "@fastify/busboy": "2.1.0" } - 检查消息字段类型:Node.js SDK对字段类型的校验比Python更严格,比如
mutableContent和contentAvailable建议改为布尔值true而非数字1,尝试修改后测试:aps: { alert: { title: title, body: content, }, mutableContent: true, contentAvailable: true } - 验证Atlas运行时版本:确认Atlas触发器使用的Node.js版本与firebase-admin版本匹配,比如Node.js 16适配firebase-admin v11.x,Node.js 18适配v12.x及以上。
内容的提问来源于stack exchange,提问作者Tom3652
相关产品推荐
相关产品推荐

