Mix Master Mike
Mix Master Mike

Reputation: 1309

Firebase Function HTTP Trigger - Error: could not handle the request - Firebase Not Defined

Apologies in advance if my syntax isn't correct as I'm a newbie to Firebase (and to node.js).

Using a HTTP trigger, I'm trying to pull 'awards' data as an object from my Realtime Database. However, based on my setup I am receiving the following error when I execute the URL that I receive from firebase deploy.

Error: could not handle the request

I did change firebase to admin as described here but that didn't resolve the issue.

Additionally I thought the .catch() method would at least give me an understanding of what the issue is vs just telling me 'error'.

I'm hoping the end result be:

{"create_your_own":"false","first_time":"true","five_in_a_row":"true", "share_with_friend": "false"}

Any help in deciphering my issue is greatly appreciated.

Functions log

enter image description here

Realtime Database

fir-db-test-mike
 |
 |__awards
 |   |
 |   |__user01
 |   |   |
 |   |   |__create_your_own: false
 |   |   |__first_time: true
 |   |   |__five_in_a_row: true
 |   |   |__share_with_friend: false
 |   |
 |   |
 |   |__user02
 |       |
 |       |__create_your_own: false
 |       |__first_time: false
 |       |__five_in_a_row: false
 |       |__share_with_friend: false

index.js

const functions = require('firebase-functions');
require('./src/grabData')(module.exports);

grabData.js

const functions = require('firebase-functions')
const admin = require('firebase-admin')
admin.initializeApp(functions.config().firebase)

26 module.exports = (event) => {
27   event.grabAwards = functions.https.onRequest((request, response) => {
28     firebase.database().ref('/awards/{pushId}').once('value')
       .then(function(snapshot) {
29       var awards = snapshot.val();
30       res.status(200).send(awards);
31       }).catch(error => {
32       this.errorMessage = 'Error - ' + error.message
33     });
34   })
35 }

Upvotes: 1

Views: 7946

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598817

You're not importing any module named firebase. Instead you import the Firebase Admin SDK as admin. So you'll want to use admin.database().

const functions = require('firebase-functions')
const admin = require('firebase-admin') // name used here
admin.initializeApp(functions.config().firebase)

module.exports = (event) => {
  event.grabAwards = functions.https.onRequest((request, response) => {
    admin.database().ref('/awards/{pushId}').once('value') // must match name used here
      .then(function(snapshot) {
        var awards = snapshot.val();
        res.status(200).send(awards);
      }).catch(error => {
        this.errorMessage = 'Error - ' + error.message
      });
  })
}

Upvotes: 1

Related Questions