Javascript Firebase云函数“日志记录不是函数”

Javascript Firebase云函数“日志记录不是函数”,javascript,node.js,firebase,firebase-realtime-database,google-cloud-functions,Javascript,Node.js,Firebase,Firebase Realtime Database,Google Cloud Functions,仅供参考,我不是本地节点。js dev 我出人意料地不断收到一个500服务错误,这是从哪里冒出来的,发生了几个小时,然后一切都重新开始工作,我没有任何努力。在谷歌搜索之后,我找不到关于错误的更多信息,但我确实找到了报告错误的信息。我遇到了这个 在我下面的代码中,当捕捉到错误时,我使用文档中的代码来报告错误,但当我运行index.js文件时,我不断得到错误: 错误:分析函数触发器时出错 TypeError:日志记录不是一个函数 错误代码: ✔ functions: Finished runnin

仅供参考,我不是本地节点。js dev

我出人意料地不断收到一个500服务错误,这是从哪里冒出来的,发生了几个小时,然后一切都重新开始工作,我没有任何努力。在谷歌搜索之后,我找不到关于错误的更多信息,但我确实找到了报告错误的信息。我遇到了这个

在我下面的代码中,当捕捉到错误时,我使用文档中的代码来报告错误,但当我运行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)
我哪里做错了

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();
    });
  });
}
根据需要,当所需的包没有默认导出时,需要导入带有括号的日志记录:

const{Logging}=require'@googlecloud/Logging'

然后使用以下命令实例化日志记录:

常量日志记录=新日志记录{projectId};//当前缺少projectId参数,根据需要,当所需的包没有默认导出时,需要导入带有括号的日志记录:

const{Logging}=require'@googlecloud/Logging'

然后使用以下命令实例化日志记录:


常量日志记录=新日志记录{projectId};//您当前缺少projectId参数

请不要发布代码的屏幕截图或其他文本内容。而是发布实际的文本,并使用Stack Overflow的格式化工具对其进行标记。好的,我将在几分钟后对其进行更改。但是屏幕截图有什么问题吗?它们无法通过搜索找到,也无法复制/粘贴到答案中。看。哦,这很有道理,谢谢你的建议。我用实际代码更新了这个问题。请参阅文档了解用法:请不要发布代码的截图或其他文本内容。而是发布实际的文本,并使用Stack Overflow的格式化工具对其进行标记。好的,我将在几分钟后对其进行更改。但是屏幕截图有什么问题吗?它们无法通过搜索找到,也无法复制/粘贴到答案中。看。哦,这很有道理,谢谢你的建议。我用实际的代码更新了这个问题。参见文档了解用法:我在@DougStevenson发布的链接中的注释下看到了这一点。我会投票给你的答案给你一些额外的分数,尽管哈哈谢谢,我是新来的!祝你的项目好运。我在@DougStevenson发布的链接评论中看到了这一点。我会投票给你的答案给你一些额外的分数,尽管哈哈谢谢,我是新来的!祝你的项目好运。
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();
    });
  });
}