我不是本地的Node.js开发人员
我出乎意料地得到了一个500 service error,它从没有出现在哪里,发生了几个小时,然后一切都开始重新开始工作,没有任何努力在我的一端。在谷歌搜索后,我找不到很多关于错误的信息,但我确实找到了说要报告错误的信息。我偶然发现了这个医生报告错误。
在下面的代码中,当在catch中捕获错误时,我使用来自文档的代码来报告它,但是当我运行我的index.js文件时,我一直得到这个错误:
错误:解析函数触发器时发生错误。 TypeError:日志不是一个函数
错误代码:
✔ 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();
});
});
}发布于 2020-11-17 05:56:58
根据医生们,您需要导入带有括号的Logging (当所需的包没有默认导出时是必要的):
const { Logging } = require('@google-cloud/logging');
然后用以下方法实例化日志记录:
const logging = new Logging({projectId}); // You're currently missing the projectId argument
https://stackoverflow.com/questions/64868429
复制相似问题