【问题标题】:Firebase get data ionic4Firebase 获取数据 ionic4
【发布时间】: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


    【解决方案1】:

    虽然您已将 getUserByEmail 声明为 async,但您并未返回承诺。

    async getUserByEmail(email: string): Promise<User> {
        const user: User = new User();
    
        return 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;
            });
    }
    

    因此,顶级现在返回 once() 返回的承诺的结果,然后返回您的用户。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多