【问题标题】:Google Cloud Function running on nodejs10 test fails在 nodejs10 上运行的 Google Cloud Function 测试失败
【发布时间】:2020-12-29 22:05:46
【问题描述】:

以下是我的功能的相关位:

    // Finally send the JSON data to the browser or requestor
    res.status(200).send(output);
  } catch (err) {
    res.status(500).send(err.message);
  } finally {
    await closeConnection(page, browser);
  }

当我在本地运行它时,它可以完美运行并将我的输出返回到网络浏览器。当我将其上传到 Google Cloud Functions 并对其进行测试时,res.status(200).send(output); 行失败并显示以下消息:

Error: function execution failed. Details:
res.status is not a function

还有其他人看到过这种行为吗?我完全不明白为什么它可以在我的本地机器上完美运行,但是当我将它作为云功能运行时却失败了。

【问题讨论】:

  • 请编辑问题以显示重现此错误的complete minimal code。你现在所拥有的还不足以理解问题所在。

标签: node.js google-cloud-platform google-cloud-functions


【解决方案1】:

在挖掘了一堆之后,我找到了答案。具有“背景”触发器类型的 Google Cloud Functions 无法识别 res.status。相反,他们想要callback

https://cloud.google.com/functions/docs/writing/background#function_parameters

    // Finally send the JSON data to the browser or requestor
    callback(null, output);
  } catch (err) {
    callback(new Error('Failed'));
  } finally {
    await closeConnection(page, browser);
  }

如果您使用 --signature-type 标志运行本地开发实例,它会正确启动,但您无法再通过在 Web 浏览器中点击端口进行测试:

    "start": "functions-framework --target=pollenCount --signature-type=cloudevent",

有关如何将模拟发布/订阅数据发送到本地实例的文档在这里:

https://cloud.google.com/functions/docs/running/calling#background_functions

【讨论】:

    猜你喜欢
    • 2021-04-25
    • 1970-01-01
    • 1970-01-01
    • 2019-08-22
    • 1970-01-01
    • 1970-01-01
    • 2020-07-02
    • 2021-05-14
    • 1970-01-01
    相关资源
    最近更新 更多