为了使用Firebase Cloud Messaging
稍后发送通知,
我正在尝试编写一个名为 sendNotification 的云函数;
我遇到了一个问题。如果可能的话,我想获得一些有关如何处理该问题的建议;或只是有人指出我可能正在做的错误。
这是运行命令时收到的错误消息:
firebase部署
⚠ functions[sendNotification(us-central1)]: Deployment error.
Function load error: Code in file index.js can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module 'serviceAccountKey.json'
at Function.Module._resolveFilename (module.js:476:15)
........
说 serviceAccountKey.json 文件位于名为 node_modules 的目录中,换句话说,不要丢失。 这是index.js中的代码:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
var serviceAccount = require('serviceAccountKey.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: 'https://myapp.firebaseio.com'
});
exports.sendNotification = functions.https.onRequest((req, res) => {
let registrationToken = "abc123.........789xyz";
var payload = {
notification: {
title: "NotifTit",
body: "The good stuff."
},
token: registrationToken
};
admin.messaging().send(payload)
.then((response) => {
console.log("Successfully sent message: ", response);
})
.catch((error) => {
console.log("Error sending message: ", error);
})
});
Here is the contents of the package.json file:
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"dependencies": {
"firebase-admin": "~6.0.0",
"firebase-functions": "^2.1.0"
},
"private": true
}
答案 0 :(得分:0)
您无法从node_modules
文件夹中加载文件,因为上载函数时该文件夹将被忽略。
Firebase开发人员Lauren Long commented:
但是要记住的一件事是整个node_modules文件夹 在上传源代码期间被忽略,因此您的package.json具有 包含所有依赖项,并且不能引用任何文件 在“ node_modules”内部。换句话说,您的功能仍应 如果您在 功能文件夹:
rm -rf node_modules
npm install