javascript - Firebase not found cloud function? -
i wanted bit testing cloud function achieve bigger goal. android developer, knowledge bit limited in case of javascript.
i trying access database firebase using cloud function.
my code access database json
response in browser.
const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeapp(functions.config().firebase); const cors = require('cors')({origin: true}); // take text parameter passed http endpoint , insert // realtime database under path /messages/:pushid/original exports.addmessage = functions.https.onrequest((req, res) => { // grab text parameter. const original = req.query.text; // push new message realtime database using firebase admin sdk. admin.database().ref('/messages').push({original: original}).then(snapshot => { // redirect 303 see other url of pushed object in firebase console. res.redirect(303, snapshot.ref); }); }); // listens new messages added /messages/:pushid/original , creates // uppercase version of message /messages/:pushid/uppercase exports.makeuppercase = functions.database.ref('/messages/{pushid}/original') .onwrite(event => { // grab current value of written realtime database. const original = event.data.val(); console.log('uppercasing', event.params.pushid, original); const uppercase = original.touppercase(); // must return promise when performing asynchronous tasks inside functions such // writing firebase realtime database. // setting "uppercase" sibling in realtime database returns promise. return event.data.ref.parent.child('uppercase').set(uppercase); }); var db = admin.database(); exports.getusermessage = functions.https.onrequest((req, res) => { var query = firebase.database().ref("messages").orderbykey(); query.once("value") .then(function(snapshot) { snapshot.foreach(function(childsnapshot) { // key "ada" first time , "alan" second time var key = childsnapshot.key; // childdata actual contents of child var childdata = childsnapshot.val(); }); }); });
but getting error :
error: not handle request
these log error firebase :
referenceerror: firebase not defined @ exports.getusermessage.functions.https.onrequest (/user_code/index.js:34:49) @ cloudfunction (/user_code/node_modules/firebase-functions/lib/providers/https.js:26:47) @ /var/tmp/worker/worker.js:635:7 @ /var/tmp/worker/worker.js:619:9 @ _combinedtickcallback (internal/process/next_tick.js:73:7) @ process._tickdomaincallback (internal/process/next_tick.js:128:9)
package.json script
{ "name": "functions", "description": "cloud functions firebase", "dependencies": { "cors": "^2.8.1", "firebase-admin": "~4.2.1", "firebase-functions": "^0.5.7" }, "private": true }
here there issue is:
var query = firebase.database().ref("messages").orderbykey();
should
db.ref("messages").orderbykey();
the firebase
you're using in line not defined in cloud functions firebase. if want query database, have reference using admin sdk:
var db = admin.database();
Comments
Post a Comment