Javascript 错误TS2739:类型';{}';类型中缺少以下属性
managestation.tsJavascript 错误TS2739:类型';{}';类型中缺少以下属性,javascript,angular,typescript,ionic-framework,Javascript,Angular,Typescript,Ionic Framework,managestation.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
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;
});
}
}
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;
}
}
managestation.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>
终端错误
src/app/members/managestation/managestation.component.ts(66,10)中出错:错误TS2739:类型“{}”缺少类型“Station”中的以下属性:id、名称、组织id、状态
控制台输出
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 []
[
[
{
"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"
}
]
]
您从API调用接收到一个空对象
{}
,并且您可能为API调用设置的数据类型为站
。而且,sta
类型是站
:sta:Station代码>。不能将空对象设置为sta
更新:
看起来您是所有数据都是可观察的。查看代码时,您还有另一个错误:
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;
}
请看属性,注意。问题是,如果你看声明,你会说这些属性中的大多数不能为null,但是如果你看构造函数,你会说相反的,哪一个是正确的?我添加了allData[0]
,它现在正在工作
getStationsByOrg(id) {
this.staService.getStationsByOrg(id)
.then((allData) => {
console.log(allData);
this.sta = allData[0];
});
}
最初的一瞥,如果您要返回一组站点,请使用以下属性:sta:Station;应为sta:Station[];尝试使用sta:Station[]代码>它仍然不工作否,数据返回不是空的,已经更新了问题AM获取数组结果<代码>getStationsByOrg(id){返回this.dataSharing.httpGetAllBy(id,'orgId','station')代码>请检查此链接@dominion是否可以创建stackblitz?这可能是无法识别对象的原因,您必须匹配属性名称和类型