Reputation: 1389
I am wanted to do some bit testing with cloud function to achieve a bigger goal. As an Android developer, my knowledge is a bit limited in the case of JavaScript.
I am trying to access this database from firebase using Cloud function.
My Code to access this database to get 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 the text parameter passed to this HTTP endpoint and insert it into the
// Realtime Database under the path /messages/:pushId/original
exports.addMessage = functions.https.onRequest((req, res) => {
// Grab the text parameter.
const original = req.query.text;
// Push the new message into the Realtime Database using the Firebase Admin SDK.
admin.database().ref('/messages').push({original: original}).then(snapshot => {
// Redirect with 303 SEE OTHER to the URL of the pushed object in the Firebase console.
res.redirect(303, snapshot.ref);
});
});
// Listens for new messages added to /messages/:pushId/original and creates an
// uppercase version of the message to /messages/:pushId/uppercase
exports.makeUppercase = functions.database.ref('/messages/{pushId}/original')
.onWrite(event => {
// Grab the current value of what was written to the Realtime Database.
const original = event.data.val();
console.log('Uppercasing', event.params.pushId, original);
const uppercase = original.toUpperCase();
// You must return a Promise when performing asynchronous tasks inside a Functions such as
// writing to the Firebase Realtime Database.
// Setting an "uppercase" sibling in the Realtime Database returns a 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 will be "ada" the first time and "alan" the second time
var key = childSnapshot.key;
// childData will be the actual contents of the child
var childData = childSnapshot.val();
});
});
});
But I am getting the error :
Error: could not handle the request
These are my Log error from Firebase :
ReferenceError: firebase is not defined
at exports.getUserMessage.functions.https.onRequest (/user_code/index.js:34:49)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/providers/https.js:26:47)
at /var/tmp/worker/worker.js:635:7
at /var/tmp/worker/worker.js:619:9
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickDomainCallback (internal/process/next_tick.js:128:9)
package.json script
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"dependencies": {
"cors": "^2.8.1",
"firebase-admin": "~4.2.1",
"firebase-functions": "^0.5.7"
},
"private": true
}
Upvotes: 3
Views: 2279
Reputation: 7566
here is there the issue is:
var query = firebase.database().ref("messages").orderByKey();
should be
db.ref("messages").orderByKey();
The firebase
you're using in that line is not defined in Cloud Functions for Firebase. If you want to query the database, you already have a reference to it using the Admin SDK:
var db = admin.database();
Upvotes: 5