Firebase错误:加载用户代码(node.js)时功能失败 [英] Firebase error: Function failed on loading user code (node.js)

查看:48
本文介绍了Firebase错误:加载用户代码(node.js)时功能失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Firebase的相对菜鸟,这是我第一次使用它,并附有教程.该教程有点过时了,我一直在修正错误,但是这个让我完全陷入了困境.我尝试运行创建文档时触发的其他功能 在某些收藏中.但是,出现以下错误:

I am a relative noob with Firebase my first time using it and following along with a tutorial. The tutorial is a bit outdated and I have been fixing bugs as I have been going, but this one has got me completely stuck. I try to run a different functions that trigger when a document is created in certain collections. However, i get the following error:

错误

!  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个与其他导出相对应的相同错误.

There are 3 more identical errors that correspond to the other exports in the following index.js file.

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);

我已按照错误提示检查了日志,并收到以下我认为无用的消息,其他功能存在其他三个相同的错误

I have checked the logs as suggested by the error and i get the following messages which i think are useless, there are three other identical errors for the other functions

错误日志

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文件

Here is my package.json file

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
}

最后,这是我的配置资料,用于初始化所有内容:

Lastly, here is my config stuff that was used to initialize everything:

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服务进行部署.现在我已经准备好部署这些触发器,这将导致严重的错误.任何帮助将不胜感激

P.S. It is worth mentioning that the http.onRequest trigger (api) was actually working and I have been developing without deploying using firebase serve. Now that I am ready to deploy these triggers something is going heinously wrong. Any help is greatly appreciated

推荐答案

我遇到了完全相同的问题,试图部署与您完全相同的代码,而我只是能够正确部署它.

I had the exact same problem, trying to deploy the exact same code as you and I was just able to get it to deploy properly.

如果您的问题与我的问题相同,那么问题出在哪里:

If yours is the same issue as mine, this is where the problem is:

var serviceAccount = require('../../service-acct/socialapp-e5130-firebase-adminsdk-uo6p6-5495e18b97.json');

在部署到Firebase时,您似乎无法使代码尝试到达项目文件夹之外,因此解决方案是将密钥放在不推荐的内部,或者将环境变量GOOGLE_APPLICATION_CREDENTIALS设置为您的文件路径服务帐户密钥json文件,如 https://firebase.google.com/docs/中所述admin/setup#windows 然后只有

It looks like when deploying to firebase you cant have code trying to reach outside your project folder, so the solution would be to have the key inside which isn't recommended or to set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the file path of your service account key json file as explained in https://firebase.google.com/docs/admin/setup#windows and then just having

admin.initializeApp();

这篇关于Firebase错误:加载用户代码(node.js)时功能失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆