简体   繁体   English

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

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

I have a Webhook that POSTs a JSON to my Cloud Function Trigger URL.我有一个 Webhook 将 JSON 发布到我的云 Function 触发器 URL。

I want the Cloud Function to parse the JSON and write it to my Cloud Firestore.我想让 Cloud Function 解析 JSON 并将其写入我的 Cloud Firestore。

I've tested the Webhook on webhook.site & requestbin.com: they are both receiving the POST request perfectly.我已经在 webhook.site 和 requestbin.com 上测试了 Webhook:它们都完美地接收了 POST 请求。

I am guessing that there is some syntax problem somewhere here, around the payload or req.body.我猜这里某处存在一些语法问题,围绕有效负载或 req.body。

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

Additionally, this is not an authenticated request, and I deployed the function through the Google Cloud Platform - Cloud Function Console.此外,这不是经过身份验证的请求,我通过 Google Cloud Platform - Cloud Function 控制台部署了 function。 I did not deploy this through the CLI, or through an application setup with firebase.我没有通过 CLI 或通过 firebase 的应用程序设置来部署它。

index.js索引.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 package.json

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

My Webhook that delivers a POST JSON to my Cloud Function URL:我的 Webhook 将 POST JSON 传送到我的云 Function URL:

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


EDIT: I've added编辑:我已经添加

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

and now my logs are returning:现在我的日志正在返回:

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

I needed to declare each field's data type first.我需要先声明每个字段的数据类型。

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

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

相关问题 如何使用复杂的 JSON Webhook 中的 Node JS Cloud Function 写入 Cloud Firestore? - How to write to the Cloud Firestore using Node JS Cloud Function from Complex JSON Webhook? 使用 Cloud Functions 将 Json Webhook 写入 Cloud Firestore。 云 Function 部署失败。 Function 加载用户代码失败 - Write Json Webhook to Cloud Firestore with Cloud Functions. Cloud Function Failed to Deploy. Function failed on loading user code 将数据从 Google Cloud 保存到 Firestore Function - Saving data to Firestore from Google Cloud Function Webhook JSON 从 Cloud Run Endpoint 返回未显示在 Dialogflow CX 中 - Webhook JSON return from Cloud Run Endpoint not showing in Dialogflow CX 使用 Firebase 云从 Cloud Firestore 读取数据 function - Read data from Cloud firestore with Firebase cloud function 用于 firestore onWrite 的云 function - Cloud function for firestore onWrite 从 Cloud Function 连接到 Cloud Firestore 的样板代码是什么? - What is a boilerplate code for connecting to Cloud Firestore from within Cloud Function? 使用 firebase 云 function 从云 Firestore 读取数据? - Read data from cloud firestore with firebase cloud function? Google Cloud Function - Python 从 Webhook 获取数据的脚本 - Google Cloud Function - Python script to get data from Webhook Cloud Firestore:编写文档参考自 Flutter Plugin/SDK - Cloud Firestore: Write document reference from Flutter Plugin/SDK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM