【问题标题】:Converting circular structure to JSON starting at object with constructor 'Subscriber'使用构造函数“订阅者”从对象开始将循环结构转换为 JSON
【发布时间】:2021-07-02 18:55:51
【问题描述】:

尝试订阅我的 observable 时出现以下错误。我不太确定为什么,因为我没有在代码中使用任何类型的 Stringify(据我所知)。它只是从一个 api 获取一个 JSON 数组。

 ERROR TypeError: Converting circular structure to JSON
        --> starting at object with constructor 'Subscriber'
        |     property '_subscriptions' -> object with constructor 'Array'
        |     index 0 -> object with constructor 'MapSubscriber'
        --- property '_parentOrParents' closes the circle
        at JSON.stringify (<anonymous>)
        at JsonPipe.transform (common.js:4752)
        at Module.ɵɵpipeBind1 (core.js:25782)
        at TicketlistComponent_Template (ticketlist.component.html:4)
        at executeTemplate (core.js:9600)
        at refreshView (core.js:9466)
        at refreshComponent (core.js:10637)
        at refreshChildComponents (core.js:9263)
        at refreshView (core.js:9516)
        at refreshEmbeddedViews (core.js:10591)

这似乎发生在从服务返回可观察对象之后

应用模块

import { NgModule, ViewChild } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatMenuModule, MatMenuTrigger } from '@angular/material/menu';
import { MatButtonModule } from '@angular/material/button';
import { MenuComponent } from './menu/menu.component';
import { MatDividerModule } from '@angular/material/divider';
import { NgbNavModule } from '@ng-bootstrap/ng-bootstrap';
import { HomeComponent } from './home/home.component';
import { TicketlistComponent } from './ticketlist/ticketlist.component';
import { TicketdetailComponent } from './ticketdetail/ticketdetail.component';
import { TicketcenterComponent } from './ticketcenter/ticketcenter.component';
import { HttpClientModule } from '@angular/common/http';


@NgModule({
  declarations: [
    AppComponent,
    MenuComponent, 
    HomeComponent, TicketlistComponent, TicketdetailComponent, TicketcenterComponent
  ],
  imports: [
    BrowserModule, 
    BrowserAnimationsModule, 
    AppRoutingModule, 
    MatMenuModule, 
    MatButtonModule, 
    MatDividerModule, 
    NgbNavModule, 
    HttpClientModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {

 }

应用组件

import { Component, OnInit } from '@angular/core';
import { HomeComponent } from '../app/home/home.component';


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit{
  title = 'Charity';

  ngOnInit(home = HomeComponent){
    
  }
  
}

应用路由模块

import { Component, NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { TicketlistComponent } from './ticketlist/ticketlist.component';

const routes: Routes = [
  { path: "ticketList", component: TicketlistComponent }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

连接服务

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { ITicketList } from '../interfaces/i-ticket-list';

@Injectable({
  providedIn: 'root'
})
export class ConnectionsService {

  constructor(private http : HttpClient) { }

  myServer: string = "https://localhost:5001/"

  getTicketList(): Observable<ITicketList[]>
  {
    console.log('getTicketList()')
    return this.http.get<ITicketList[]>(this.myServer + "Tickets/getTickets")
  } 
}

票务列表组件

import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { ITicketList } from '../interfaces/i-ticket-list';
import { ConnectionsService } from '../services/connections.service';

@Component({
  selector: 'app-ticketlist',
  templateUrl: './ticketlist.component.html',
  styleUrls: ['./ticketlist.component.scss']
})
export class TicketlistComponent implements OnInit {

  constructor(private connService : ConnectionsService) { }

  getTickets$ : Observable<ITicketList[]> | any;

  getTickets()
  {
    console.log('getTickets')
    this.getTickets$ = this.connService.getTicketList().subscribe();
  }

  ngOnInit(): void {

    console.log('ngOnInit')
    this.getTickets();

  }

}

票务列表html

<p>ticketlist works!</p>

<!-- <div *ngIf="getTickets$ | async as getTickets; else loading"> -->
    {{ getTickets$ | json }}
<!-- </div> -->

<!-- <ng-template #loading>
    loading...
</ng-template> -->

ITicketList(接口)

export interface ITicketList {

    ticketId : string, 
    customerId: string, 
    createdOn: Date, 
    statusId: number, 
    statusDesc: string

}

样本负载

[{"ticketId":"b4d8e061-1bd9-eb11-8f59-9408532f1cc6","customerId":"b3d8e061-1bd9-eb11-8f59-9408532f1cc6","createdOn":"2021-06-29T16:48:40.44","statusId":1,"statusDesc":"Open"}]

【问题讨论】:

    标签: angular typescript rxjs circular-reference


    【解决方案1】:

    通过读取错误调用堆栈,我认为是这一行:

    {{ getTickets$ | json }}
    

    你不能 json 管道 Observable 流。

    它应该是这样的:

    <div *ngIf="getTickets$ | async as getTickets; else loading">
        {{ getTickets | json }}
    </div>
    

    【讨论】:

    • 嗯...是和否...它解决了该错误,但现在我收到错误错误错误:InvalidPipeArgument: '[object Object]' for pipe 'AsyncPipe' at invalidPipeArgumentError (common .js:4212) 在 AsyncPipe._selectStrategy (common.js:4314) 在 AsyncPipe._subscribe (common.js:4304) 在 AsyncPipe.transform (common.js:4292)
    • at Module.ɵɵpipeBind1 (core.js:25782) at TicketlistComponent_Template (ticketlist.component.html:3) at executeTemplate (core.js:9600) at refreshView (core.js:9466) at refreshComponent (core.js:10637) 在 refreshChildComponents (core.js:9263) 建议? (或者我应该在另一个帖子中发布它吗?)
    • 所以看起来问题出在“Subscribe()”这个词上。没有它它工作正常。但这很奇怪。我认为 Observables 需要订阅
    • 异步管道*自动*为您订阅和取消订阅。这是它的好处之一。
    • 观看此 youtube 视频了解更多信息:youtube.com/watch?t=869&v=7AvI6F8ifqU(相关部分为 45 分钟)
    猜你喜欢
    • 2021-02-20
    • 2020-01-31
    • 2021-07-23
    • 1970-01-01
    • 2016-01-11
    • 1970-01-01
    • 2017-04-03
    • 2018-11-01
    • 1970-01-01
    相关资源
    最近更新 更多