Angular 错误类型错误:client.country.map不是函数
我有2个JSON,我想在html中显示名称,而不是id_conutryAngular 错误类型错误:client.country.map不是函数,angular,typescript,Angular,Typescript,我有2个JSON,我想在html中显示名称,而不是id_conutry Country: <!-- language: lang-json --> { "StatusCode": 0, "StatusMessage": "OK", "StatusDescription": [ { "country_id": 5, "name": "afghanistan", "short_name": "af" }, {
Country:
<!-- language: lang-json -->
{
"StatusCode": 0,
"StatusMessage": "OK",
"StatusDescription": [
{
"country_id": 5,
"name": "afghanistan",
"short_name": "af"
},
{
"country_id": 6,
"name": "albania",
"short_name": "al"
},
{
"country_id": 7,
"name": "algeria",
"short_name": "dz"
},
{
"country_id": 8,
"name": "american samoa",
"short_name": "as"
},
{
"country_id": 9,
"name": "andorra",
"short_name": "ad"
},
{
"country_id": 10,
"name": "angola",
"short_name": "ao"
},
.....
{
"country_id": 314,
"name": "countrytestcreated",
"short_name": "supt"
}
]
}
因此,我想在HTML中显示的是:
客户名称:测试,
电邮:123@live.com,
姓名:安道尔
我还参加了以下课程:
导出类客户端{
客户端id:字符串;
客户端名称:字符串;
联系人编号:字符串;
注:字符串;
电子邮件:字符串;
活动:数字;
国家:国家[];
}
出口类国家{
国家/地区标识:字符串;
名称:字符串;
}
我试图在我的表格中修补价值。为此,我尝试了以下代码:
populateForm(){
this.activatedRoute.params.subscribe(
参数=>{
this.clientService.clientgetbyid(params['id'])。订阅(
客户=>{
this.client=client;
this.editClientForm.controls['client_name'].setValue(client.clientName);
this.editClientForm.controls['email'].setValue(client.email);
this.editClientForm.patchValue({
country\u id:client.country.map(x=>x.name)
})
}
);
}
);
}
我的服务:
publicclientgetbyid(id:string):可观察{
// ...
返回这个.http.post(Api.getUrl(Api.url.clientgetbyid)),body{
标题:标题
})
.map((响应:响应)=>{
让res=response.json();
console.log(res)//正确显示我的数据
如果(res.StatusCode==0){
返回新客户端(res.StatusDescription[0]);
}否则如果(res.StatusCode==1){
this.auth.logout();
}否则{
返回新客户端(空);
}
});
}
我得到的错误是:
错误类型错误:client.country.map不是函数
我的html表单:
<form [formGroup]="editClientForm">
<div class="row">
<div class="input-field col s12">
<input formControlName="client_name" id="client_name" type="text" class="validate" required=true>
<label for="client_name">Client Name</label>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<input formControlName="contactNo" id="contactNo" type="number" class="validate">
<label for="contactNo">Contact Number</label>
</div>
</div>
<input formControlName="country_id" id="country_id" matInput placeholder="Select Country*" aria-label="State" [matAutocomplete]="auto"
autoActiveFirstOption [formControl]="country">
<mat-autocomplete #auto="matAutocomplete" >
<mat-option (onSelectionChange)="updateForm($event, country.country_id, 'country_id')" *ngFor="let country of filteredOptionsCountry | async"
[value]="country.name">
{{ country.name }}
</mat-option>
</mat-autocomplete>
<div id="edit_client_button_container" class="row">
<button [type="submit" class="btn waves-effect waves-light">
Update
</button>
</div>
</form>
客户名称
联系电话
{{country.name}
更新
你能给我提出解决办法吗?谢谢当您打印回复时,您的回复中不存在国家/地区数组,因此为了避免错误,您应该通过执行以下操作来检查它是否存在
if(client.country && client.country.length > 0)
{
this.editClientForm.patchValue({
country_id: client.country.map(x => x.name)
});
}
我的答案是基于您的日志输出,没有国家/地区数组
{
"StatusCode": 0,
"StatusMessage": "OK",
"StatusDescription": [
{
"client_id": "1",
"client_name": "test test",
"contactno": "123",
"note": "Test again
"email": "123@live.com",
"country_id": 9,
"active": 1
}
]
}
请提供HTTP调用的响应日志。在您的JSON中,我们看不到一个
country
property。您好,您能在订阅方法中登录client
,并在响应中显示它的输出吗object@trichetriche我编辑了我的帖子。我仍然没有在你的帖子中看到任何数组。在html中显示空输入。请再次查看我的帖子,我用html代码编辑,只有国家id为空,其他输入显示correctly@OnaBi-您需要单独获取国家/地区列表,因为现在您在获取clientAha的当前调用中没有使用它,当我单击“自动完成”时,我的国家/地区显示在输入中。但在本期中,我希望在调用客户端时立即显示。
{
"StatusCode": 0,
"StatusMessage": "OK",
"StatusDescription": [
{
"client_id": "1",
"client_name": "test test",
"contactno": "123",
"note": "Test again
"email": "123@live.com",
"country_id": 9,
"active": 1
}
]
}