【发布时间】:2019-11-08 04:05:10
【问题描述】:
无法从返回对象中获取数据。我可以从 firebase 实时数据库中获取类,但所有属性仍未定义。 但是如果我使用 console.log() 就不再有 undefined 了。
constructor(public alertController: AlertController,
private userService: UsersService,
private apartmentService: ApartmentService) {
this.userService.currentUser().then(
(user) => {
if (user) {
this.currentUser = user;
console.log(this.currentUser); // defined
console.log('Apartment Key = ' + this.currentUser.apartmentKey); // Undefined
if (user.apartmentKey !== null && user.apartmentKey !== '') {
this.apartmentService.getApartment(this.currentUser.apartmentKey).then((apartment) => {
if (apartment) {
apartment.users.forEach(value => {
this.userService.getUserByEmail(value).then(userByEmail => {
this.users.push(userByEmail);
});
});
}
});
}
}
}
);
}
Firebase 索引在“电子邮件”上
"rules": {
".read": true,
".write": true,
"my-users": {
".indexOn": "email"
}
}
以及获取用户的服务方法
async getUserByEmail(email: string): Promise<User> {
const user: User = new User();
this.fireBase.database.ref(Path.USER_PATH)
.orderByChild('email')
.equalTo(email)
.once('value')
.then((snapshot) => {
snapshot.forEach(dataSnap => {
if (dataSnap) {
const data = dataSnap.val();
user.email = data.email;
user.apartmentKey = data.apartmentKey;
}
});
});
return user;
}
这是控制台输出:
【问题讨论】:
标签: typescript ionic-framework firebase-realtime-database