I have wrote a Firebase cloud function that can be accessed via a REST API. The code is given below.
What I need to do is, when the user submits some values from the front end via the 'Web service URL' .
1.) I need these data to be saved in the Firebase-realtime database.
2.) I refereed to several tutorials on the net, and didn't understand what var ref = db.ref("server/saving-data/fireblog"); does in the following code.
const functions = require('firebase-functions');
const gcs = require('@google-cloud/storage')();
const os = require("os");
const path = require("path");
const cors = require("cors")({ origin: true });
var admin = require("firebase-admin");
var serviceAccount = require("./My-Service.json");
// Initialize the app with a service account, granting admin privileges
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://my-xx0xx.firebaseio.com/"
});
// As an admin, the app has access to read and write all data, regardless of Security Rules
var db = admin.database();
var ref = db.ref("server/saving-data/fireblog");
exports.uploadFile = functions.https.onRequest((req, res) => {
cors(req, res, () => {
var usersRef = ref.child("users");
usersRef.set({
alanisawesome: {
date_of_birth: "June 23, 1912",
full_name: "Alan Turing"
},
gracehop: {
date_of_birth: "December 9, 1906",
full_name: "Grace Hopper"
}
});
if (req.method !== 'POST') {
return res.status(500).json({
message: 'Not allowed'
});
}
res.status(200).json({
message: req.body
});
});
});
