我是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个与其他导出相对应的相同错误。
Index.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文件
Package.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
}
最后,这是我用来初始化所有内容的配置资料:
admin.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);
P.S.值得一提的是,http.onRequest触发器(api)实际上正在工作,并且我一直在开发而不使用Firebase服务进行部署。现在我已经准备好部署这些触发器,这将导致严重的错误。任何帮助是极大的赞赏

最佳答案

我遇到了完全相同的问题,尝试部署与您完全相同的代码,我才能够正确部署它。
如果您的问题与我的问题相同,那么这里的问题是:

var serviceAccount = require('../../service-acct/socialapp-e5130-firebase-adminsdk-uo6p6-5495e18b97.json');
似乎在部署到Firebase时,您无法使代码尝试到达项目文件夹之外,因此解决方案是将 key 放在不推荐的内部,或者将环境变量GOOGLE_APPLICATION_CREDENTIALS设置为服务帐户 key 的文件路径https://firebase.google.com/docs/admin/setup#windows中说明的json文件,然后
admin.initializeApp();

关于node.js - Firebase错误: Function failed on loading user code (node. js),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62843260/

10-11 23:09
查看更多