javascript - How do I return a snapshot.val() from Firebase to a variable? - Stack Overflow

var getListings = function () {listingsRef.once("value").then(function(snapshot) {console.log

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

I have the following method. console.log(snapshot.val()) 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()

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

I have the following method. console.log(snapshot.val()) 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()

Share Improve this question asked Oct 6, 2016 at 5:21 VDogVDog 1,1752 gold badges16 silver badges36 bronze badges 5
  • Typing from my phone, but I wonder if you declare a variable at the very top of the get Listings function, then set it equal to snapshot.val inside your other function, THEN return that variable at the very end of get Listings, if that would work. It would be important to make sure the return was outside of the nested function BTW – Dan Zuzevich Commented Oct 6, 2016 at 5:38
  • The usual pattern is to return the promise. Not exactly sure of promise implementation in firebase. – sabithpocker Commented Oct 6, 2016 at 5:42
  • @DanielZuzevich nah, this is the first thing I tried, this returns an empty string or undefined, because it's loading asynchronously, and I'm assigning the variable to nothing right away >_>. – VDog Commented Oct 6, 2016 at 5:54
  • Terrifying. I'll see if I can pull something together. Was just doing this in React with firebase. Granted it's a different result your looking for. – Dan Zuzevich Commented Oct 6, 2016 at 6:03
  • yoooo, peep one of my questions i had answered on firebase a few weeks ago. Never know, this might e up in your other pursuits. stackoverflow./questions/39600061/… – Dan Zuzevich Commented Oct 6, 2016 at 7:03
Add a ment  | 

3 Answers 3

Reset to default 3

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){
    console.log(e);
}

function init(){
    loadListing();
}

The other solution is the older way of using callbacks. This is not remended as it can lead to unmanageable code if there are multiple nested async calls. Promises are a solution to the mess created by callbacks.

var currentSnapshot;
function getListings() {
    listingsRef.once("value", setListing, showError);
};

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

function showError(e){
    console.log(e);
}

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

Does this sorcery work at all?

nevermind, this for some reason doesn't work either... although it should?

var getListings = function () {
    var currentItems = [];
    listingsRef.on("value", function(snapshot) {
        currentItems.push(snapshot.val());
    })
    , function (errorObject) {
        console.log("The read failed: " + errorObject.code);
    };

    return currentItems;
};

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745372530a4624857.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信