猿问

错误:“对象”类型的“[对象对象]”。

我正在尝试使用 Ionic 和 AngularFire 从 Firebase Firestore 数据库中获取我用户的用户名。我正在使用该valueChanges()方法来获取 observable,然后尝试使用异步管道解释 observable。但是,当我运行代码时,出现以下错误:

错误

但是,当我记录 observable 时,它显示如下:

可观察的

profile.page.ts:

import { Component, OnInit } from '@angular/core';


import { AngularFireAuth } from '@angular/fire/auth';

import { Router } from '@angular/router';


import { UserService } from '../user.service'


import { AngularFirestore, AngularFirestoreCollection } from '@angular/fire/firestore'


import { Observable } from 'rxjs';


@Component({

  selector: 'app-profile',

  templateUrl: './profile.page.html',

  styleUrls: ['./profile.page.scss'],

})


export class ProfilePage implements OnInit {






  constructor(public afAuth: AngularFireAuth, public router: Router, public user: UserService, public db: AngularFirestore) { 



  }




  ngOnInit() {




  }


  logout() { this.afAuth.auth.signOut(); this.router.navigate(['/login']);}



}

profile.page.html:


 <ion-content>

  <ion-grid>

      <ion-row justify-content-center align-items-center style="height: 50%">

    <ion-button color="danger" size="small" shape="round" (click)="logout()">Logout</ion-button>

    <p *ngFor="let users of (username | async)">{{ users.username }}</p>

    </ion-row>

    </ion-grid>

</ion-content>  

在此先感谢您的帮助。


米琪卡哇伊
浏览 142回答 2
2回答

蝴蝶不菲

您遇到该错误是因为您的用户名数据不是数组,因此我建议您像这样更改代码。使您的用户名成为一个数组,然后将其推入数组username: string[] = [];this.username = this.username.push(users.valueChanges());

HUWWW

您应该尝试使用 for of 将来自您的服务的映射结果推送/取消移动到一个数组,并返回该数组,例如:private itemsCollection: AngularFirestoreCollection<User>&nbsp; &nbsp; &nbsp;this.itemsCollection = this.afs.collection<User>('user', ref => ref.orderBy('name','desc').limit(5));&nbsp; &nbsp; &nbsp; &nbsp; return this.itemsCollection.valueChanges().pipe(map( (users: User[]) =>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let users = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (const user of users) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.users.unshift(user);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return users&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }))afs 是 AngularFirestore 类型
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答