ERROR TypeError: Converting circular structure to JSON --> starting at object with constructor 'FirebaseAppImpl' Firebase Angular
ERROR TypeError: Converting circular structure to JSON --> starting at object with constructor 'FirebaseAppImpl' Firebase Angular
当尝试从可观察对象获取数据时,我从控制台收到此错误。
我的代码如下
Anglar 服务 - service.ts
import { Injectable } from '@angular/core';
import { AngularFirestore } from '@angular/fire/firestore';
@Injectable({
providedIn: 'root'
})
export class ViewReportService {
constructor(private firestore: AngularFirestore) { }
getReport = (myDocument) =>
this.firestore.collection("report").doc(myDocument).get()
}
组件 ts.
import { Component, OnInit } from '@angular/core';
import { ViewReportService } from '../shared/view-report.service';
import { Observable } from 'rxjs';
@Component({
selector: 'app-view-report',
templateUrl: './view-report.component.html',
styleUrls: ['./view-report.component.scss']
})
export class ViewReportComponent implements OnInit {
document= [];
document$ : Observable<any>;
constructor(private service: ViewReportService) { }
ngOnInit(){
let documentID = "----"
//Get the Data from the view-report service
this.document$ = this.service.getReport(documentID);
}
}
在我的 HTML 视图中
<table *ngIf="document$ | async as document">
<pre>{{document.name}}</pre>
听起来你有一个循环对象结构,然后不能存储在数据库中。请记住:Firestore 文档只能包含 JSON 数据,并非所有 JavaScript 对象都是有效的 JSON。例如:JavaScript 对象可能包含函数定义,这些定义在 JSON.
中无效
将 JavaScript 对象转换为 JSON 的最简单方法是 JSON.parse(JSON.stringify(object))
。您需要在将对象写入数据库的地方执行此操作。
当尝试从可观察对象获取数据时,我从控制台收到此错误。
我的代码如下
Anglar 服务 - service.ts
import { Injectable } from '@angular/core';
import { AngularFirestore } from '@angular/fire/firestore';
@Injectable({
providedIn: 'root'
})
export class ViewReportService {
constructor(private firestore: AngularFirestore) { }
getReport = (myDocument) =>
this.firestore.collection("report").doc(myDocument).get()
}
组件 ts.
import { Component, OnInit } from '@angular/core';
import { ViewReportService } from '../shared/view-report.service';
import { Observable } from 'rxjs';
@Component({
selector: 'app-view-report',
templateUrl: './view-report.component.html',
styleUrls: ['./view-report.component.scss']
})
export class ViewReportComponent implements OnInit {
document= [];
document$ : Observable<any>;
constructor(private service: ViewReportService) { }
ngOnInit(){
let documentID = "----"
//Get the Data from the view-report service
this.document$ = this.service.getReport(documentID);
}
}
在我的 HTML 视图中
<table *ngIf="document$ | async as document">
<pre>{{document.name}}</pre>
听起来你有一个循环对象结构,然后不能存储在数据库中。请记住:Firestore 文档只能包含 JSON 数据,并非所有 JavaScript 对象都是有效的 JSON。例如:JavaScript 对象可能包含函数定义,这些定义在 JSON.
中无效将 JavaScript 对象转换为 JSON 的最简单方法是 JSON.parse(JSON.stringify(object))
。您需要在将对象写入数据库的地方执行此操作。