I am new in firebase environment. I want to update data in the firebase using cloud function using angular service. I am able to fetch data in angular from firebase using cloud function as follows
readUsers(): Observable<Person[]>{
return this._http
.get(“https://us-central1-partner-f3f0b.cloudfunctions.net/getCustomer")
.map(res => res.json());
}
And cloud fuction is as follows
exports.getCustomer = functions.https.onRequest((req, res) => {
res.header(‘Content-Type’, ‘application/json’);
res.header(‘Access-Control-Allow-Origin’, ‘*’);
res.header(‘Access-Control-Allow-Headers’, ‘Content-Type’);
//respond to CORS preflight requests
if (req.method === ‘OPTIONS’) {
res.status(204).send(‘’);
}
// Get a database reference to our posts
var db = admin.database();
var ref = db.ref(“customer”);
// Attach an asynchronous callback to read the data at our posts reference
ref.on(“value”, function (snapshot) {
console.log(snapshot.val());
res.status(200).json({ customer: snapshot.val() });
}, function (errorObject) {
console.log(“The read failed: “ + errorObject.code);
});
});
The below cloud function is working and updating the firebase database .
exports.getOneProducts = functions.https.onRequest((req, res) => {
res.header(‘Content-Type’, ‘application/json’);
res.header(‘Access-Control-Allow-Origin’, ‘*’);
res.header(‘Access-Control-Allow-Headers’, ‘Content-Type’);
//respond to CORS preflight requests
if (req.method === ‘OPTIONS’) {
res.status(204).send(‘’);
}
// Get a database reference to our posts
var db = admin.database();
var ref = db.ref(“/messages/-L82AfhFTr4odsh1GMId”);
ref.update({ original: “New trainer” });
});
I am facing the challenge here how to pass data in json format in angular 4 and update in firebase database using google cloud function . Any help is appreciated.