从firebase 3检索我的数据
问题描述:
我一直在试图从firebase 3获取我的数据,并想知道是否有人知道我正在创建的错误。从firebase 3检索我的数据
服务:
.service("searchService", function($firebaseArray, fb) {
var database = firebase.database();
var bookId = firebase.auth().currentUser;
this.getPostedBooks = function() {
return database.ref("postedBooks/" + bookId).once("value");
}
控制器:
$scope.getPostedBooks = function() {
searchService.getPostedBooks().then(function(response) {
$timeout(function() {
console.log(response.val())
}, 5000);
})
我试图使用超时来查看数据是否晚点进来的,但无论怎样,我只是得到空。
答
你可以尝试$ Q服务
https://docs.angularjs.org/api/ng/service/ $ Q
.service("searchService", function($firebaseArray, $q, fb) {
var database = firebase.database();
var bookId = firebase.auth().currentUser;
var dfd = $q.defer();
return {
getPostedBooks : function() {
return database.ref("postedBooks/" + bookId).once("value", function(snapshot) {
dfd.resolve(snapshot.val());
}, function (errorObject) {
//log error
});
return dfd.promise;
}
}
})