【问题标题】:error TS2739: Type '{}' is missing the following properties from type错误 TS2739:类型“{}”缺少类型中的以下属性
【发布时间】:2020-04-07 02:38:20
【问题描述】:

ma​​nagestation.ts

import { Component, OnInit } from '@angular/core';
import { Station } from 'src/app/_models/station';
import { StationService } from 'src/app/_services/station.service';
import { AddStaModalComponent } from 'src/app/modal/station/add-sta-modal/add-sta-modal.component';
import { EditStaModalComponent } from 'src/app/modal/station/edit-sta-modal/edit-sta-modal.component';
import { DataSharingService } from 'src/app/_services/data-sharing.service';
import { ActivatedRoute, Router, NavigationEnd } from '@angular/router';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

@Component({
  selector: 'app-managestation',
  templateUrl: './managestation.component.html',
  styleUrls: ['./managestation.component.scss'],
})
export class ManagestationComponent implements OnInit {
  sta: Station;
  mySubscription: any;

  constructor(
    private dataSharing: DataSharingService,
    private staService: StationService,
    private activatedRoute: ActivatedRoute,
    private router: Router
  ) {}
  getStationsByOrg(id) {
      this.staService.getStationsByOrg(id)
      .then((allData) => {
        console.log(allData);
         this.sta = allData;
      });
    }
}

ma​​nagestation.component.html

<ion-content class="body">
  <ion-button expand="block" (click)="onAddStation()">Add Station
    <ion-icon name='add' slot="end"></ion-icon>
  </ion-button>
  <ion-list>
    <ion-item *ngFor="let s of sta">
      <ion-label>{{ s.name }}</ion-label>
      <ion-label>{{ s.orgId }}</ion-label>
      <ion-icon name='create' slot="end" (click)="onEditStation(s.id)"></ion-icon>
      <ion-icon name='trash' slot="end" (click)="onDeleteStation(s.id)"></ion-icon>
    </ion-item>
  </ion-list>
</ion-content>

station.ts

import { StationStatus } from './station-status.enum';

export class Station {
    id: number;
    name: string;
    orgId: number;
    address?: string;
    status: StationStatus;

    constructor(id?: number, name?: string, orgId?: number, address?: string, status?: StationStatus) {
        this.id = id;
        this.name = name;
        this.orgId = orgId;
        this.address = address;
        this.status = status;
    }
}

终端错误

src/app/members/managestation/managestation.component.ts(66,10) 中的错误:错误 TS2739:“{}”类型缺少“Station”类型的以下属性:id、name、orgId、status

控制台输出

 Array [ (4) […] ]
managestation.component.ts:65:16

(1) […]
​
0: (4) […]
​​
0: Object { id: 5, orgId: 2, name: "BitCo Gas Jos North", … }
​​
1: Object { id: 6, orgId: 2, name: "BitCo Gas Awolowo Road ", … }
​​
2: Object { id: 7, orgId: 2, name: "BitCo Gas Kano Central", … }
​​
3: Object { id: 8, orgId: 2, name: "BitCo Gas Efik", … }
​​
length: 4
​​
<prototype>: Array []
​
length: 1
​
<prototype>: Array []

我在 console.log 中得到结果,但我无法在我的 html 组件文件中使用它

结果

[
  [
    {
      "id": 9,
      "orgId": 3,
      "name": "Conap Corp Ikeja",
      "address": "23, Ogunlowo Street, Obafemi Awolowway. Ikeja, Lagos",
      "status": "active"
    },
    {
      "id": 10,
      "orgId": 3,
      "name": "Conap Corp Ota",
      "address": "23, Ogunlowo Street, Obafemi Awolowway. Ikeja, Lagos",
      "status": "active"
    },
    {
      "id": 11,
      "orgId": 3,
      "name": "Conap Corp Agbara",
      "address": "23, Ogunlowo Street, Obafemi Awolowway. Ikeja, Lagos",
      "status": "active"
    },
    {
      "id": 12,
      "orgId": 3,
      "name": "Conap Corp Agbado",
      "address": "23, Ogunlowo Street, Obafemi Awolowway. Ikeja, Lagos",
      "status": "active"
    }
  ]
]

【问题讨论】:

  • 初看,如果你得到一个站数组返回,这个属性: sta: Station;应该是 sta: Station[];
  • 尝试使用sta: Station[];还是不行

标签: javascript angular typescript ionic-framework


【解决方案1】:

我添加了allData[0],它现在可以工作了

  getStationsByOrg(id) {
      this.staService.getStationsByOrg(id)
      .then((allData) => {
        console.log(allData);
         this.sta = allData[0];
      });
    }

【讨论】:

    【解决方案2】:

    您从 API 调用中收到一个空对象 {},并且您可能为 API 调用设置的数据类型是 Station。此外,sta 类型为 Station:sta: Station;。您不能将空对象设置为 sta

    更新:

    看起来你是 allData 是一个 observable。

    【讨论】:

    • 否,数据返回不为空,已更新问题
    • 我得到一个数组作为结果
    • getStationsByOrg(id) { return this.dataSharing.httpGetAllBy(id, 'orgId', 'station') };
    • @dominion 是否可以创建堆栈闪电战?
    【解决方案3】:

    您还有另一个错误,查看您的代码:

    export class Station {
        id: number;
        name: string;
        orgId: number;
        address?: string;
        status: StationStatus;
    
        constructor(id?: number, name?: string, orgId?: number, address?: string, status?: StationStatus) {
            this.id = id;
            this.name = name;
            this.orgId = orgId;
            this.address = address;
            this.status = status;
        }
    

    查看属性,请注意。问题是,如果你看声明,你是说那些属性大多数不能为空,但如果你看构造函数,你说的是相反的,哪个是正确的?

    【讨论】:

    • 这可能是无法识别对象的原因,您必须匹配属性名称和类型
    猜你喜欢
    • 2020-01-30
    • 2021-05-27
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    • 2021-01-05
    • 1970-01-01
    • 2021-04-20
    • 2021-07-29
    相关资源
    最近更新 更多