Cloud Function 无法解析 JSON 并无法从 Webhook 写入 Cloud Firestore
Cloud Function failing to parse JSON and write to Cloud Firestore from Webhook
我有一个 Webhook POSTJSON 我的 Cloud Function Trigger URL。
我希望 Cloud Function 解析 JSON 并将其写入我的 Cloud Firestore。
我已经在 webhook.site 和 requestbin.com 上测试了 Webhook:它们都完美地接收了 POST 请求。
我猜这里某处存在一些语法问题,围绕有效负载或 req.body。
exports.wooCommerceWebhook = async (req, res) => {
const payload = req.body;
此外,这不是经过身份验证的请求,我通过 Google 云平台 - 云功能控制台部署了该功能。我没有通过 CLI 或通过使用 firebase 的应用程序设置来部署它。
index.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 向我的 Cloud Function POST JSON 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 = "";
我有一个 Webhook POSTJSON 我的 Cloud Function Trigger URL。
我希望 Cloud Function 解析 JSON 并将其写入我的 Cloud Firestore。
我已经在 webhook.site 和 requestbin.com 上测试了 Webhook:它们都完美地接收了 POST 请求。
我猜这里某处存在一些语法问题,围绕有效负载或 req.body。
exports.wooCommerceWebhook = async (req, res) => {
const payload = req.body;
此外,这不是经过身份验证的请求,我通过 Google 云平台 - 云功能控制台部署了该功能。我没有通过 CLI 或通过使用 firebase 的应用程序设置来部署它。
index.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 向我的 Cloud Function POST JSON 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 = "";