javascript - 错误:“对象”类型的“[对象对象]”。NgFor 仅支持绑定到 Iterables,例如 Arrays。- 离子项目
问题描述
我正在尝试使用 Ionic 和 AngularFire 从 Firebase Firestore 数据库中获取我的用户名。我正在使用该valueChanges()
方法获取可观察对象,然后尝试使用异步管道解释可观察对象。但是,当我运行代码时,出现以下错误:
但是,当我记录 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>
提前感谢您的帮助。
解决方案
你有这个错误,因为你的用户名数据不是一个数组,所以我建议你像这样更改你的代码。使您的用户名成为一个数组,然后将其推入数组
username: string[] = [];
this.username = this.username.push(users.valueChanges());
推荐阅读
- javascript - 如果我点击表格的外侧,如何隐藏菜单
- java - 使用 Jetty Websocket 客户端时如何抑制将日志输出到标准输出?
- ajax - 以编程方式在 actionlistener 中添加 ajax 行为
- javascript - 来自nodeJS脚本的python shell向nodejs返回相同的值
- c++ - 关于 assistant-sdk-cpp 上的 API 文档和设备操作
- unity3d - 由于缺少 Origin 标头 + CORS,从 S3 下载 Unity3D WebGL 网络失败
- kubernetes - 从 Kubernetes pod 获取自定义指标
- c++ - Visual C++ 从文件中保存/读取多个变量
- html - 孤立的结束标签 HTML5 会发生什么
- ms-access - 在 KeyDown 事件中访问 VBA 位掩码