0

I'm trying to retrieve some data from the Firebase database, and at the same time export this data as a module because it will be reused in other parts of the project. The code below returns an undefined object.

const admin = require('firebase-admin');
const firebaseDatabase = admin.database();
const firebaseRef = firebaseDatabase.ref('users');

/**
 * @description
 * Retrieves names from Firebase db
 * @param {String} id
 */
module.exports = function(id){
  firebaseRef.on('value', function(snapshot) {
    return snapshot.val()[id];
  }, function(err) {
    return false;
  });
};
Bargain23
  • 1,863
  • 3
  • 29
  • 50
  • 1
    Related: _[How do I return the response from an asnychronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call)_ – Luca Kiebel May 04 '18 at 18:36
  • If you want to access data a single time, you should be using `once()` instead of `on()`. `on()` establishes a listener whose callback will be invoked with every change, which is probably not what you want here. They are both asynchronous, and you will have to use asynchronous programming techniques to deal with their results (you can't just `return` data from an async function). – Doug Stevenson May 04 '18 at 18:38

0 Answers0