如何将 Firebase 查询的结果存储到承诺的变量中?
How to store the result of a Firebase query to a variable from a promise?
我是 angular 的新手,仍在努力掌握承诺。
我有一个 ItemDetail class:
@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;
_auth.getUser(this.navParams.get('item').author).then(function(snapshot){
console.log("ayyy lmao = " + JSON.stringify(snapshot.child(navParams.get('item').author).val().email));
this.author = JSON.stringify(snapshot.child(navParams.get('item').author).val().email);
});
console.log("thisAuthor = " + this.author);
}
}
我正在尝试将从数据库中检索到的电子邮件存储为作者变量。即使它在控制台中正确输出,它的值实际上并没有分配给 promise 之外的变量。但是我认为我遇到的问题与承诺有关,这是一个我仍在努力理解的概念。
Authclass中的getUser方法如下:
getUser(uid: string): any {
return firebase.database().ref('/userProfile').orderByKey().equalTo(uid).once("value", function(snapshot){});
}
如果有更好的方法(无需承诺),那也是一个不错的选择。
谢谢!
似乎承诺工作正常,但 this
指向错误的实例,使用 Function.prototype.bind
将 this
绑定到 ItemDetailPage:
function successHandler(snapshot){ ... };
_auth.getUser(this.navParams.get('item').author).then(successHandler.bind(this));
一些事情...
获取用户配置文件信息的更好方法是在插入对象时使用 uid 作为键
firebase.database().ref('/userProfile/' + uid).once("value", function(snapshot){});
如果您使用 =>
fat arrows,绑定会在 ionic2 中为您完成
_auth.getUser(this.navParams.get('item').author)
.then((snapshot) => {
// THIS WILL BE WHAT YOU EXPECT...
});
我是 angular 的新手,仍在努力掌握承诺。
我有一个 ItemDetail class:
@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;
_auth.getUser(this.navParams.get('item').author).then(function(snapshot){
console.log("ayyy lmao = " + JSON.stringify(snapshot.child(navParams.get('item').author).val().email));
this.author = JSON.stringify(snapshot.child(navParams.get('item').author).val().email);
});
console.log("thisAuthor = " + this.author);
}
}
我正在尝试将从数据库中检索到的电子邮件存储为作者变量。即使它在控制台中正确输出,它的值实际上并没有分配给 promise 之外的变量。但是我认为我遇到的问题与承诺有关,这是一个我仍在努力理解的概念。
Authclass中的getUser方法如下:
getUser(uid: string): any {
return firebase.database().ref('/userProfile').orderByKey().equalTo(uid).once("value", function(snapshot){});
}
如果有更好的方法(无需承诺),那也是一个不错的选择。
谢谢!
似乎承诺工作正常,但 this
指向错误的实例,使用 Function.prototype.bind
将 this
绑定到 ItemDetailPage:
function successHandler(snapshot){ ... };
_auth.getUser(this.navParams.get('item').author).then(successHandler.bind(this));
一些事情...
获取用户配置文件信息的更好方法是在插入对象时使用 uid 作为键
firebase.database().ref('/userProfile/' + uid).once("value", function(snapshot){});
如果您使用 =>
fat arrows,绑定会在 ionic2 中为您完成
_auth.getUser(this.navParams.get('item').author)
.then((snapshot) => {
// THIS WILL BE WHAT YOU EXPECT...
});