Sur*_*raj 12 node.js firebase google-cloud-functions google-cloud-firestore
我是 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
Run Code Online (Sandbox Code Playgroud)
还有 3 个相同的错误对应于以下 index.js 文件中的其他导出。
索引.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);
Run Code Online (Sandbox Code Playgroud)
我已经按照错误的建议检查了日志,我收到了以下我认为没用的消息,其他功能还有其他三个相同的错误
错误日志
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"}
Run Code Online (Sandbox Code Playgroud)
这是我的 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
}
Run Code Online (Sandbox Code Playgroud)
最后,这是我用于初始化所有内容的配置内容:
管理.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 }
Run Code Online (Sandbox Code Playgroud)
Firebase 初始化
const firebase = require('firebase');
const config = require('../util/config.js');
firebase.initializeApp(config);
Run Code Online (Sandbox Code Playgroud)
PS 值得一提的是,http.onRequest 触发器(api)实际上正在工作,我一直在开发,而没有使用 firebase serve 进行部署。现在我已经准备好部署这些触发器了。任何帮助是极大的赞赏
小智 18
输入此命令以获取日志:
firebase functions:log
Run Code Online (Sandbox Code Playgroud)
小智 8
我遇到了完全相同的问题,试图部署与您完全相同的代码,我只是能够正确部署它。
如果你的问题和我的一样,那么问题就出在这里:
var serviceAccount = require('../../service-acct/socialapp-e5130-firebase-adminsdk-uo6p6-5495e18b97.json');
Run Code Online (Sandbox Code Playgroud)
看起来在部署到 firebase 时,您无法让代码尝试访问您的项目文件夹之外,因此解决方案是将不推荐的密钥放入其中,或者将环境变量 GOOGLE_APPLICATION_CREDENTIALS 设置为您的服务帐户密钥的文件路径json 文件,如https://firebase.google.com/docs/admin/setup#windows 中所述,然后只需
admin.initializeApp();
Run Code Online (Sandbox Code Playgroud)
小智 7
我在设置 SendGrid 时遇到了同样的问题。
我收到此错误是因为我不小心将 SendGrid 安装到根文件夹而不是函数文件夹。
确保使用 cmd 提示符将服务安装到函数文件夹中。它应该看起来像这样:
D:\firebaseProject\functions> npm i @sendgrid/mail
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6717 次 |
| 最近记录: |