Yağız Ozan Dikbaş
Yağız Ozan Dikbaş

Reputation: 29

I can not read the data on Firebase Functions

I use firebase functions for push notification triggers.

I can send notifications but I cannot read some data when the trigger launches.

I tried usual firebase database functions but they didn't work.

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

let fcm = ''
exports.sendNewMessageNotification = functions.database.ref('/xxx/userInformations/{uid}/')
    .onWrite((change:any) => {
    // console.log(change.before)
      const payload = {
          notification: {
              title: 'Kahve Falı',
              body: 'Falın geldi!',
      }
    }
    admin.database().ref('/xxx/userInformations/{uid}/fortunes')
    .once('value')
    .then((snapshot:any) => {
      console.log('snapshot.val()')//I want to get that snaphot.val()

      });
      fcm = change.before._data.fcm;
      return admin.messaging().sendToDevice(fcm, payload);
    });

It doesn't read that snapshot. It returns null or undefined with different ways. And I can not make this line like that; .then((snapshot) => {

If I don't put :any it gives me an error on VS Code.

Upvotes: 0

Views: 454

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 599621

You seem to be mistaking two parts of your code.


First you have:

exports.sendNewMessageNotification = functions.database.ref('/xxx/userInformations/${uid}/')

This declares a Cloud Function. When you deploy this code with Firebase, it recognizes this Cloud Function and interprets the string literal '/xxx/userInformations/${uid}/' as the path that this code needs to trigger on. It puts the values after /xxx/userInformations in the path into a parameter called uid.


Next you have:

admin.database().ref('/xxx/userInformations/${uid}/fortunes')

This is regular JavaScript code, in which case '/xxx/userInformations/${uid}/fortunes' is just a string that is sent to the Firebase client. Since $ is not allowed in a path, the Firebase client raises an error.

You probably want the path to include the value of the uid parameter, which you can do with:

admin.database().ref(`/xxx/userInformations/${context.params.uid}/fortunes`)

So the changes:

  1. Use backticks to mark the string, so that it's interpreted as a ES6 Template Literal.
  2. Use ${context.params.uid} in there, to get the value of uid that was used to trigger the code.

This requires that you define context in your Cloud Function declations, like:

.onWrite((change:any, context: any) => {

Upvotes: 1

Abdulhaq Shah
Abdulhaq Shah

Reputation: 552

The issue is in template literal ${expression}. You are not providing uid correctly.

Use template literal if you are using ES6 or you can use + like concatenate the uid with url.

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

let fcm = ''
exports.sendNewMessageNotification = functions.database.ref('/xxx/userInformations/${uid}/')
    .onWrite((change:any) => {
    // console.log(change.before)
      const payload = {
          notification: {
              title: 'Kahve Falı',
              body: 'Falın geldi!',
      }
    }
    admin.database().ref(`/xxx/userInformations/${uid}/fortunes`)
    .once('value')
    .then((snapshot:any) => {
      console.log('snapshot.val()')//I want to get that snaphot.val()

      });
      fcm = change.before._data.fcm;
      return admin.messaging().sendToDevice(fcm, payload);
    });

Upvotes: 1

Related Questions