【发布时间】:2016-11-08 09:13:29
【问题描述】:
我在身份验证提供程序中有一个 getUser 方法(在 Ionic 2 中工作):
getUser(uid: string): any {
var toReturn;
firebase.database().ref('/userProfile').orderByKey().equalTo(uid).once("value", function(snapshot){
toReturn = snapshot.val();
console.log("getUser = " + JSON.stringify(toReturn));
});
return toReturn;
}
它从 firebase 检索用户对象。 如果它存在并保存在 toReturn 变量中,它会通过 console.log 正确输出。
下面是 ItemDetailPage 类,但是当我记录相同方法的值时,它返回 undefined。
import { Component } from '@angular/core';
import { NavParams} from 'ionic-angular';
import { Auth } from '../../providers/auth/auth';
@Component({
templateUrl: 'build/pages/item-detail/item-detail.html',
providers: [Auth]
})
export class ItemDetailPage {
private title;
private description;
private author;
constructor(private navParams: NavParams, private _auth: Auth) {
this.title = this.navParams.get('item').title;
this.description = this.navParams.get('item').description;
this.author = _auth.getUser(this.navParams.get('item').author);
console.log("item-detail.ts = " + JSON.stringify(this.author));
//_auth.getUser('123');
}
}
我是 Angular 的新手,并且在不断尝试学习,所以这可能是我错过的一些非常简单的事情。但是,为什么 JSON 对象没有被返回/传回?我需要它来利用从数据库中查询的值。
谢谢
【问题讨论】:
-
你应该使用 lambda 语法 (snapshot) => {} 而不是 function(snapshot) {}。看看它是否有效。
标签: angularjs ionic-framework firebase firebase-realtime-database ionic2