Alex
Alex

Reputation: 1188

How to update database via HTTP trigger function in firebase?

I have one problem to develop Firebase application. I wrote a HTTP trigger function to update database when it's triggered but not sure how to do it.

I think this is a quiet easy problem.

Thank you.

[EDIT]

exports.email = functions.https.onRequest((req, res) => {

      functions.database.ref("test/appointment").once('value',(snapshots)=>{
        console.log('value is',snapshots.val());
        res.end()
      })

});

I tried like above but it's not working.

Upvotes: 0

Views: 659

Answers (1)

Rosário P. Fernandes
Rosário P. Fernandes

Reputation: 11326

As Doug mentioned in the comments, that's not valid.

If you want to access the database from your HTTP Triggered function, you'll need to you use the Server Admin SDK. Your code would look as follows:

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

exports.email = functions.https.onRequest((req, res) => {

      const appointmentRef = admin.database().child('test/appointment');

      appointmentRef.once('value',(snapshots)=>{
        console.log('value is',snapshots.val());
        res.end()
      })

});

Upvotes: 1

Related Questions