admin管理员组文章数量:1426201
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()
- 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
3 Answers
Reset to default 3Return 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;
};
本文标签: javascriptHow do I return a snapshotval() from Firebase to a variableStack Overflow
版权声明:本文标题:javascript - How do I return a snapshot.val() from Firebase to a variable? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745372529a2655793.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论