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.
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.
This Function does require HTTPS.
I was able to get my function to write to the Firestore, but it did not write to the fields within a Map - I've included screenshots at the bottom to show what my Firestore looks like within the Firebase / Google Cloud Platform Console.
What syntax do I need to provide to make sure my Cloud Function takes the JSON and writes to the Firestore while respecting the Map data type?
Do I need to declare people_Email = map? and if I do, how would that be accomplished?
index.js
const admin = require('firebase-admin')
admin.initializeApp();
exports.wooCommerceWebhook = async (req, res) => {
const payload = req.body;
var billing = ""; // Do I even need to declare every nest of the complex JSON?
var people_EmailHome = "";
var people_FirstName = "";
var people_LastName = "";
// Write to Firestore - People Collection
await admin.firestore().collection("people").doc().set({
people_EmailHome: 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"
}
}
Screenshots of my Cloud Firestore
CodePudding user response:
What syntax do I need to provide to make sure my Cloud Function takes the JSON and writes to the Firestore while respecting the Map data type?
The following will do the trick:
const admin = require('firebase-admin')
admin.initializeApp();
exports.wooCommerceWebhook = async (req, res) => {
const payload = req.body;
await admin.firestore().collection("people").doc().set({
people_Email: { people_EmailHome: payload.billing.email },
people_Names: { people_FirstName: payload.billing.first_name, people_LastName: payload.billing.last_name }
});
return res.status(200).end();
};