【问题标题】:getting error while deploying firebase cloud pub-sub code部署firebase云发布-订阅代码时出错
【发布时间】:2021-07-28 00:35:47
【问题描述】:

在 firebase 上部署 pub-sub 代码时出现以下错误:

ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Function failed on loading user code. This is likely due to a bug in the user code. Error message: Code in file index.js can't be loaded.
Is there a syntax error in your code?
Detailed stack trace: /srv/node_modules/@google-cloud/pubsub/build/src/pubsub.js:527
    async *listSchemas(view = schema_1.SchemaViews.Basic, options) {
          ^

我不明白为什么会发生此错误。

以下是代码:

exports.publish = async (req, res) => {
  if (!req.body.topic || !req.body.message) {
    res.status(400).send('Missing parameter(s); include "topic" and "message" properties in your request.');
    return;
  }

  console.log(`Publishing message to topic ${req.body.topic}`);

  const topic = pubsub.topic(req.body.topic);

  const messageObject = {
    data: {
      message: req.body.message,
    },
  };
  const messageBuffer = Buffer.from(JSON.stringify(messageObject), "utf8");

  try {
    await topic.publish(messageBuffer);
    res.status(200).send("Message published.");
  } catch (err) {
    console.error(err);
    res.status(500).send(err);
    return Promise.reject(err);
  }
};

【问题讨论】:

  • 你能分享你的代码吗?
  • @Dharmaraj 我已经添加了代码
  • 您在何时何地致电publish?这是一个 HTTP 触发函数还是在数据库上的文档更改后触发了这个函数?请您详细说明一下吗?
  • @JuanGiacosa 我正在参考文档cloud.google.com/functions/docs/calling/pubsub

标签: javascript node.js firebase google-cloud-functions google-cloud-pubsub


【解决方案1】:

我猜这个函数设置为使用 Node 8 运行时,因为在 Node 10 中添加了对异步迭代器的支持。Pub/Sub 库从 2.0 开始只支持 Node 10 及更高版本,所以碰到了Firebase 函数的运行时版本应该会有所帮助:

https://firebase.google.com/docs/functions/manage-functions#set_nodejs_version

很遗憾,我没有足够的分数来询问有关原始问题的更多详细信息,但希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2021-02-27
    • 2020-03-26
    • 1970-01-01
    • 2015-11-29
    • 1970-01-01
    • 2021-12-03
    • 2018-08-03
    • 2021-10-05
    • 2018-09-21
    相关资源
    最近更新 更多