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

Lan*_*ria 1 javascript node.js firebase firebase-realtime-database google-cloud-functions

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

\n

我出乎意料地不断收到500 service error莫名其妙的消息,持续了几个小时,然后一切又开始工作,我不需要任何努力。在谷歌搜索后,我找不到太多有关该错误的信息,但我确实找到了要报告该错误的信息。我遇到这个文档来报告错误

\n

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

\n
\n

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

\n

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

\n
\n

错误代码:

\n
\xe2\x9c\x94  functions: Finished running predeploy script.\ni  functions: ensuring required API cloudfunctions.googleapis.com is enabled...\ni  functions: ensuring required API cloudbuild.googleapis.com is enabled...\n\xe2\x9c\x94  functions: required API cloudbuild.googleapis.com is enabled\n\xe2\x9c\x94  functions: required API cloudfunctions.googleapis.com is enabled\ni  functions: preparing functions directory for uploading...\n\nError: Error occurred while parsing your function triggers.\n\nTypeError: Logging is not a function\n    at Object.<anonymous> (/Users/lance/Cloud_Functions/functions/index.js:12:17)\n    at Module._compile (internal/modules/cjs/loader.js:776:30)\n    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)\n    at Module.load (internal/modules/cjs/loader.js:653:32)\n    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)\n    at Function.Module._load (internal/modules/cjs/loader.js:585:3)\n    at Module.require (internal/modules/cjs/loader.js:690:17)\n    at require (internal/modules/cjs/helpers.js:25:18)\n    at /usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:15:15\n    at Object.<anonymous> (/usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:53:3)\n
Run Code Online (Sandbox Code Playgroud)\n

我检查了我的package.json文件,"@google-cloud/logging": "^8.1.1"图书馆就在那里。

\n

包.json:

\n
{\n  "name": "functions",\n  "description": "Cloud Functions for Firebase",\n  "scripts": {\n    "lint": "eslint .",\n    "serve": "firebase emulators:start --only functions",\n    "shell": "firebase functions:shell",\n    "start": "npm run shell",\n    "deploy": "firebase deploy --only functions",\n    "logs": "firebase functions:log"\n  },\n  "engines": {\n    "node": "12"\n  },\n  "main": "index.js",\n  "dependencies": {\n    "@google-cloud/logging": "^8.1.1",\n    "firebase-admin": "^9.2.0",\n    "firebase-functions": "^3.11.0",\n    "save": "^2.4.0"\n  },\n  "devDependencies": {\n    "eslint": "^5.12.0",\n    "eslint-plugin-promise": "^4.0.1",\n    "firebase-functions-test": "^0.2.0"\n  },\n  "private": true\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我哪里出错了?

\n

索引.js:

\n
const functions = require(\'firebase-functions\');\nconst admin = require(\'firebase-admin\');\nadmin.initializeApp();\n\nconst Logging = require(\'@google-cloud/logging\');\nconst logging = Logging();\n\nexports.runTest = functions.https.onRequest((request, response) => {\n\n    return refOne.update({ "...": "..."})\n    .then(() => { \n        return refTwo.set(admin.database.ServerValue.increment(1));\n    })\n    .catch((error) => {\n                    \n        return reportError(error);\n    });\n});\n\nfunction reportError(err, context = {}) {\n  // This is the name of the StackDriver log stream that will receive the log\n  // entry. This name can be any valid log stream name, but must contain "err"\n  // in order for the error to be picked up by StackDriver Error Reporting.\n  const logName = \'errors\';\n  const log = logging.log(logName);\n\n  // https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/MonitoredResource\n  const metadata = {\n    resource: {\n      type: \'cloud_function\',\n      labels: { function_name: process.env.FUNCTION_NAME },\n    },\n  };\n\n  // https://cloud.google.com/error-reporting/reference/rest/v1beta1/ErrorEvent\n  const errorEvent = {\n    message: err.stack,\n    serviceContext: {\n      service: process.env.FUNCTION_NAME,\n      resourceType: \'cloud_function\',\n    },\n    context: context,\n  };\n\n  // Write the error log entry\n  return new Promise((resolve, reject) => {\n    log.write(log.entry(metadata, errorEvent), (error) => {\n      if (error) {\n        return reject(error);\n      }\n      return resolve();\n    });\n  });\n}\n
Run Code Online (Sandbox Code Playgroud)\n

小智 5

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

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

然后实例化日志记录:

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