Firebase Cloud 函数'Logging is not a function'



FYI我不是本机Node.js开发

我意外地得到了一个不知从哪里冒出来的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"库就在那里。

Package.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
}

我哪里出错了

Index.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

相关内容

最新更新