首页 > 解决方案 > 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 进行部署。现在我已准备好部署这些触发器,但出现了严重的错误。任何帮助是极大的赞赏

标签: node.jsfirebasegoogle-cloud-firestoregoogle-cloud-functions

解决方案


输入此命令以获取日志:

firebase functions:log

推荐阅读