node.js - Firebase 错误:加载用户代码(node.js)时函数失败
问题描述
我是第一次使用 Firebase 并遵循教程的相对菜鸟。该教程有点过时,我一直在修复错误,但是这个让我完全陷入困境。我尝试运行在某些集合中创建文档时触发的不同函数。但是,我收到以下错误:
错误
! functions[createNotificationOnlike(us-central1)]: Deployment error.
Function failed on loading user code. Error message: Error: please examine your function logs to see the error cause: https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs
在以下 index.js 文件中,还有 3 个相同的错误对应于其他导出。
索引.js
exports.createNotificationOnlike = functions.firestore.document('likes/{id}').onCreate(async (snapshot) => {
try {
const doc = await db.doc(`posts/${snapshot.data().postId}`).get(); // we have access to user handle via the likes route
if(doc.exists){
await db.doc(`notifications/${snapshot.id}`).set({ // the id of the like is the same as the id of the notification that pertains to the like
createdAt: new Date().toISOString,
recipient: doc.data.userHandle,
sender: snapshot.data().userHandle,
type: 'like',
read: false,
postId: doc.id
});
return;
}
} catch (err) {
console.error(err);
return;
}
});
exports.removeNotificationOnUnlikePost = functions.firestore.document('likes/{id}').onDelete( async (snapshot) => {
try {
await db.doc(`notifications/${snapshot.id}`).delete();
return;
} catch (err) {
console.error(err);
return;
}
})
exports.createNotificationForComments = functions.firestore.document('comments/{id}').onCreate(async (snapshot) => {
try {
const doc = await db.doc(`posts/${snapshot.data().postId}`).get();
if(doc.exists){
db.doc(`notifications/${snapshot.id}`).set({
createdAt: new Date().toISOString,
recipient: doc.data.userHandle,
sender: snapshot.data().userHandle,
type: 'comment',
read: false,
postId: doc.id
})
return;
}
} catch (err) {
console.error(err);
return; // we dont need return messages since this isnt an endpoint it is a db trigger event
}
})
// auto turn the app into base route url/api
exports.api = functions.https.onRequest(app);
我已经按照错误的建议检查了日志,我收到了以下我认为没用的消息,其他功能还有其他三个相同的错误
错误日志
removeNotificationOnUnlikePost
{"@type":"type.googleapis.com/google.cloud.audit.AuditLog","status":{"code":3,"message":"Function failed on loading user code. Error message: Error: please examine your function logs to see the error cause: https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs"}
这是我的 package.json 文件
包.json
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "10"
},
"dependencies": {
"busboy": "^0.3.1",
"express": "^4.17.1",
"firebase": "^7.16.0",
"firebase-admin": "^8.10.0",
"firebase-functions": "^3.6.1"
},
"devDependencies": {
"firebase-functions-test": "^0.2.0"
},
"private": true
}
最后,这是我用于初始化所有内容的配置内容:
管理员.js
const admin = require('firebase-admin');
var serviceAccount = require('../../service-acct/socialapp-e5130-firebase-adminsdk-uo6p6-5495e18b97.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
storageBucket: "socialapp-e5130.appspot.com",
databaseURL: "https://socialapp-e5130.firebaseio.com"
});
const db = admin.firestore();
module.exports = { db, admin }
Firebase 初始化
const firebase = require('firebase');
const config = require('../util/config.js');
firebase.initializeApp(config);
PS 值得一提的是,http.onRequest 触发器(api)实际上正在工作,而且我一直在开发而不使用 firebase serve 进行部署。现在我已准备好部署这些触发器,但出现了严重的错误。任何帮助是极大的赞赏
解决方案
输入此命令以获取日志:
firebase functions:log
推荐阅读
- azure - 使用基于分隔符的 Azure 数据工厂拆分文件
- r - 使用 dplyr 将新计算分组到一个数据帧中
- python - 为什么 discord.py 机器人不通过语音聊天播放音频?
- python - Python Tkinter 标签未正确移动
- go - JetBrains GoLand。如何终止程序?
- python - python pdfminer 挂起一些 url。有谁知道为什么?
- amazon-web-services - 从 main.tf 配置创建多个 SQS
- python - Python 模拟返回无效值
- c - 测试设置的多个位
- node.js - npm 错误:404 Not Found 获取 https://registry.npmjs.org/@progress/kendo-charts/-/kendo-charts-1.19.1.tgz