javascript - How to get a value from Firebase Database and assign it to a value in Firebase Cloud Functions? -
so i'm using firebase backend demo app, , stack quite bit, i'm hung on 1 issue, can't figure out how retrieve values database (i've got keys fine).
put, i'm sending information 1 device contains "friendly-name" of device trying reach firebase function. function code looks this:
exports.connectme = functions.https.onrequest((req, res) => { cors(req, res, () => { admin.database().ref('calls/' + req.body.id + '/').set({ target: req.body.target, caller: req.body.caller, time: req.body.time }); const target = req.body.target; console.log(`target: ${target}`); const targetcall = admin.database().ref(`tokens/${target}/token`); console.log(targetcall); // const targetvalue = targetcall.val(); res.status(200).send("thanks call!"); }); });
the variable, targetcall correctly pointed @ db entry want reach, cannot extract value life of me. token located @ admin.database.ref
required placed in http request. how value? commented out code shows variable want store in, know .val() not method of reference.
record, admin
set require('firebase-admin');
earlier in code.
you close getting database query work.
take @ firebase documentation. explains how access structured data.
exports.connectme = functions.https.onrequest((req, res) => { cors(req, res, () => { const callref = admin.database().ref('calls/' + req.body.id + '/').set({ target: req.body.target, caller: req.body.caller, time: req.body.time }); const target = req.body.target; console.log(`target: ${target}`); const takenref = admin.database().ref(`tokens/${target}/token`) .once('value'); promise.all([callref, takenref]) .then(results => { const snapshot = results[1]; // promise of `takenref` console.log(snapshot.val()); // data database... res.status(200).send("thanks call!"); }); }); });
see updated code, i've added .once('value').then(snapshot
onto query gives access data.
Comments
Post a Comment