VDog VDog - 1 year ago 172
Javascript Question

How do I return a snapshot.val() from Firebase to a variable?

var getListings = function () {
listingsRef.once("value").then(function(snapshot) {
}, function (errorObject) {
console.log("The read failed: " + errorObject.code);

I have the following method.
is working as expected. However, if I
return snapshot.val()
it returns undefined. I cannot seem to figure out how to do
var currentSnapshot = getListings()

Answer Source

Return a Promise from get listing. Consume the Promise using resolve and reject functions.

var getListings = function () {
    return listingsRef.once("value");

var currentSnapshot;

function loadListing(){
    getListings.then(setListing, showError);

function setListing(snapshot){
    currentSnapshot = snapshot.val()

function showError(e){

function init(){
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download