gpt4 book ai didi

javascript - 类型 A 缺少类型 B 的以下属性 : a, b

转载 作者:行者123 更新时间:2023-11-28 03:22:10 25 4
gpt4 key购买 nike

我很难修复此错误:

Type 'Mahasiswa[]' is missing the following properties from type 'MahasiswaResult': info, result

我在这里尝试做的是执行 GET 请求并将数据获取到下面的 HTML。这是user-detail.component.html:

<div class="container-fluid pt-3 pb-3">
<p>Current NIM: {{currentNIM}}</p>
<p>Ini Halaman Detailnya Khusus Untuk ::</p>
<p>{{ mahasiswa.result.alamat }} ~~ {{ mahasiswa.result.nama_lengkap }} -- {{ mahasiswa.result.email }}</p>

以下是链接到 HTML 的代码:user-detail.component.ts:

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { PelayanApiService } from '../_shared/services/pelayan-api.service';
import { MahasiswaResult } from '../_shared/models/mahasiswa-result';
@Component({
selector: 'app-user-detail',
templateUrl: './user-detail.component.html',
styleUrls: ['./user-detail.component.css']
})
export class UserDetailComponent implements OnInit {
public mahasiswa: MahasiswaResult;
currentNIM: string;
constructor(private pelayan: PelayanApiService,
public activatedRoute: ActivatedRoute) { }

ngOnInit() {
this.currentNIM = this.activatedRoute.snapshot.paramMap.get("nim");
this.pelayan.getMahasiswaByNim(this.currentNIM).subscribe(result => {this.mahasiswa = result; console.log(this.mahasiswa)});
}

}

pelayan-api.service.ts:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

import { Mahasiswa } from '../models/mahasiswa';
@Injectable({
providedIn: 'root'
})
export class PelayanApiService {

private apiDocumentation = 'https://documenter.getpostman.com/view/5658787/SW7W5pjd';
private urlApi = 'https://umn-pti2019.herokuapp.com';

constructor(private http: HttpClient) { }

getAllMahasiswa(): Observable<Mahasiswa> {
return this.http.get<Mahasiswa>(`${this.urlApi}/api/mahasiswa`);
}
getMahasiswaByNim(nim: string): Observable<Mahasiswa[]> {
return this.http.get<Mahasiswa[]>(`${this.urlApi}/api/mahasiswa/${nim}`);
}
}

mahasiswa-result.ts:

export interface MahasiswaResult {
info: string;
result: {
alamat: string;
angkatan: string;
created_at: string;
email: string;
foto: string;
id: string;
nama_lengkap: string;
nim: string;
prodi: string;
tanggal_lahir: string;
telepon: string;
updated_at: string;
};
}

以下是获取的 API 示例:

{"info":"Univ Student 🤔","result":{"id":"1","nim":"13536","email":"aaa@gmail.com","nama_lengkap":"aaa","foto":"https://i.pximg.net/img-original/img/2019/11/07/03/56/06/77690982_p0.png","created_at":"1573751507000","updated_at":"1573764954857","telepon":"0818181818181","alamat":"Sleman, Yogyakarta","prodi":"Inf","tanggal_lahir":"1995-08-21","angkatan":"2014"}}

最佳答案

您正在从服务中的以下函数返回 Mahasiswa[]

getMahasiswaByNim(nim: string): Observable<Mahasiswa[]> {
return this.http.get<Mahasiswa[]>(`${this.urlApi}/api/mahasiswa/${nim}`);
}

尝试将其更改为

getMahasiswaByNim(nim: string): Observable<MahasiswaResult> {
return this.http.get<MahasiswaResult>(`${this.urlApi}/api/mahasiswa/${nim}`);
}

关于javascript - 类型 A 缺少类型 B 的以下属性 : a, b,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59042197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com