繁体   English   中英

Cloud Function 无法解析 JSON 并从 Webhook 写入 Cloud Firestore

[英]Cloud Function failing to parse JSON and write to Cloud Firestore from Webhook

我有一个 Webhook 将 JSON 发布到我的云 Function 触发器 URL。

我想让 Cloud Function 解析 JSON 并将其写入我的 Cloud Firestore。

我已经在 webhook.site 和 requestbin.com 上测试了 Webhook:它们都完美地接收了 POST 请求。

我猜这里某处存在一些语法问题,围绕有效负载或 req.body。

exports.wooCommerceWebhook = async (req, res) => {
    const payload = req.body;

此外,这不是经过身份验证的请求,我通过 Google Cloud Platform - Cloud Function 控制台部署了 function。 我没有通过 CLI 或通过 firebase 的应用程序设置来部署它。

索引.js

const admin = require('firebase-admin')
admin.initializeApp();

exports.wooCommerceWebhook = async (req, res) => {
    const payload = req.body;

    // Write to Firestore - People Collection
    await admin.firestore().collection("people").doc().set({
        people_Email: payload.billing.email,
        people_FirstName: payload.billing.first_name,
        people_LastName: payload.billing.last_name,
    });

    return res.status(200).end();

};

package.json

{
  "name": "sample-http",
  "version": "0.0.1",
  "dependencies": {
      "firebase-admin": "^9.4.2"
  }
}

我的 Webhook 将 POST JSON 传送到我的云 Function URL:

{
     "billing": {
          "email": "test@test.com",
          "first_name": "First",
          "last_name": "Last"
     }
}


编辑:我已经添加

.catch((err) => { console.log(err); })

现在我的日志正在返回:

Unhandled rejection
TypeError: Cannot read property 'email' of undefined at exports.wooCommerceWebhook (/workspace/index.js:18:43)
at /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:98:17
at processTicksAndRejections (internal/process/task_queues.js:77:11)
Function execution took 599 ms, finished with status: 'crash'
Error detected in test-function-1

我需要先声明每个字段的数据类型。

let billing = "";
let people_Email = "";
let people_FirstName = "";
let people_LastName = "";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM