0
votes

I want to read all child nodes of a node ( let us say messages) and a json for data is shown below. So should get xyz1, xyz2, xyz3 ....

{
  "messages" : {
    "-M5eDLlTbnvcXT2kfuYQ" : {
      "original" : "xyz1"
    },
    "-M5eDf8iY2c0emnbiw_R" : {
      "original" : "xyz2"
    },
    "-M5eDl2SZ2dDc8UWd8-4" : {
      "original" : "xyz3"
    }
  }
}

I tried following code

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';

admin.initializeApp(functions.config().firebase);

export const repeat = functions.https.onCall(function (data, context) {

    console.log(' begin ');
    admin.database().ref('messages').once('value').then(
        function (snapshot) {
            console.log(' snapshotRead ' + snapshot.val().toString());
        }
    );
    console.log(' do further ');
}
);

but gets error 'Promises must be handled appropriately' on firebase deploy for line 'admin.database().ref...'

What is the correct way to read all values ?

1

1 Answers

0
votes

According to the documentation on sending back the result:

To return data after an asynchronous operation, return a promise. The data returned by the promise is sent back to the client.

Minimally, you will have to do something like this:

    return admin.database().ref('messages').once('value').then(snapshot => {
        return snapshot.val();
    });