FYI I'm not a native Node.js dev
I unexpectedly kept getting a 500 service error
that came out of no where, occurred for several hours, then everything started to work just again without any effort on my end. After googling around I couldn't find much info about the error but I did find info that said to report it. I came across this doc to report errors .
In my code below when an error is caught in the catch
, I use the code from the docs to report it but when I run my index.js
file I keep getting the error:
Error: Error occurred while parsing your function triggers.
TypeError: Logging is not a function
Error Code:
✔ 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)
I checked inside my package.json
file and the "@google-cloud/logging": "^8.1.1"
library is there.
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
}
Where am I going wrong?
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();
});
});
}
According to the docs you need to import Logging
with brackets around it (necessary when the required package doesn't have a default export):
const { Logging } = require('@google-cloud/logging');
then instantiate logging with:
const logging = new Logging({projectId}); // You're currently missing the projectId argument
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.