繁体   English   中英

Firebase 云功能“日志记录不是功能”

[英]Firebase cloud functions 'Logging is not a function'

仅供参考,我不是本地 Node.js 开发人员

我出乎意料地不断收到500 service error ,该500 service error不知从何而来,持续了几个小时,然后一切又重新开始工作,我没有任何努力。 在谷歌搜索之后,我找不到关于错误的太多信息,但我确实找到了说要报告它的信息。 我遇到了这个文档来报告错误

在下面的代码中,当catch捕获到错误时,我使用文档中的代码来报告它,但是当我运行我的index.js文件时,我不断收到错误消息:

错误:解析函数触发器时出错。

类型错误:日志记录不是函数

错误代码:

✔  functions: Finished running predeploy script.
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
✔  functions: required API cloudbuild.googleapis.com is enabled
✔  functions: required API cloudfunctions.googleapis.com is enabled
i  functions: preparing functions directory for uploading...

Error: Error occurred while parsing your function triggers.

TypeError: Logging is not a function
    at Object.<anonymous> (/Users/lance/Cloud_Functions/functions/index.js:12:17)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:690:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at /usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:15:15
    at Object.<anonymous> (/usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:53:3)

我检查了我的package.json文件,里面有"@google-cloud/logging": "^8.1.1"库。

包.json:

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "eslint .",
    "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": "12"
  },
  "main": "index.js",
  "dependencies": {
    "@google-cloud/logging": "^8.1.1",
    "firebase-admin": "^9.2.0",
    "firebase-functions": "^3.11.0",
    "save": "^2.4.0"
  },
  "devDependencies": {
    "eslint": "^5.12.0",
    "eslint-plugin-promise": "^4.0.1",
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}

我哪里错了?

索引.js:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

const Logging = require('@google-cloud/logging');
const logging = Logging();

exports.runTest = functions.https.onRequest((request, response) => {

    return refOne.update({ "...": "..."})
    .then(() => { 
        return refTwo.set(admin.database.ServerValue.increment(1));
    })
    .catch((error) => {
                    
        return reportError(error);
    });
});

function reportError(err, context = {}) {
  // This is the name of the StackDriver log stream that will receive the log
  // entry. This name can be any valid log stream name, but must contain "err"
  // in order for the error to be picked up by StackDriver Error Reporting.
  const logName = 'errors';
  const log = logging.log(logName);

  // https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/MonitoredResource
  const metadata = {
    resource: {
      type: 'cloud_function',
      labels: { function_name: process.env.FUNCTION_NAME },
    },
  };

  // https://cloud.google.com/error-reporting/reference/rest/v1beta1/ErrorEvent
  const errorEvent = {
    message: err.stack,
    serviceContext: {
      service: process.env.FUNCTION_NAME,
      resourceType: 'cloud_function',
    },
    context: context,
  };

  // Write the error log entry
  return new Promise((resolve, reject) => {
    log.write(log.entry(metadata, errorEvent), (error) => {
      if (error) {
        return reject(error);
      }
      return resolve();
    });
  });
}

根据文档,您需要使用括号导入Logging (当所需的包没有默认导出时是必需的):

const { Logging } = require('@google-cloud/logging');

然后实例化日志记录:

const logging = new Logging({projectId}); // You're currently missing the projectId argument

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM