I have a Webhook that POSTs a JSON to my Cloud Function Trigger URL.
I want the Cloud Function to parse the JSON and write it to my Cloud Firestore.
I've tested the Webhook on webhook.site & requestbin.com : they are both receiving the POST request perfectly.
I am guessing that there is some syntax problem somewhere here, around the payload or 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. I did not deploy this through the CLI, or through an application setup with 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"
}
}
My Webhook that delivers a POST JSON to my Cloud Function URL:
{
"billing": {
"email": "[email protected]",
"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
CodePudding user response:
I needed to declare each field's data type first.
let billing = "";
let people_Email = "";
let people_FirstName = "";
let people_LastName = "";