[英]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.