最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - error showing in my .ts code > [ts] Property 'name' does not exist on type 'any[]&am

programmeradmin1浏览0评论

My coding working fine. data showing on page, but an error accruing in VSE editor

Like this

[ts] Property 'name' does not exist on type 'any[]'.

my .ts file code is here

ngOnInit() {
    const data = {
      tailor_id: this.edit_id,
      user: this.userToken
    };
    this.https.post<any>('api/tailor/details', data).subscribe(response => {
      this.tailor = response.tailor;
      this.editTailorForm.controls['name'].setValue(this.tailor.name);
      this.editTailorForm.controls['phone_number'].setValue(this.tailor.phone_number);
    });

  }

My coding working fine. data showing on page, but an error accruing in VSE editor

Like this

[ts] Property 'name' does not exist on type 'any[]'.

my .ts file code is here

ngOnInit() {
    const data = {
      tailor_id: this.edit_id,
      user: this.userToken
    };
    this.https.post<any>('api/tailor/details', data).subscribe(response => {
      this.tailor = response.tailor;
      this.editTailorForm.controls['name'].setValue(this.tailor.name);
      this.editTailorForm.controls['phone_number'].setValue(this.tailor.phone_number);
    });

  }

Share Improve this question asked Jan 29, 2019 at 6:57 HarryHarry 1332 silver badges11 bronze badges 2
  • Can you show me the declaration of this.tailor? – officialMKL Commented Jan 29, 2019 at 7:00
  • 1 You have declared trailor : any[], which is an array, So you should be accessing it like this.tailor[0].name – Amit Chigadani Commented Jan 29, 2019 at 7:06
Add a ment  | 

1 Answer 1

Reset to default 5

You have declared tailor: any[], which is an array, So you should be accessing it like this.tailor[0].name.

If tailor is an object, then declare it as tailor: any

And access like this.tailor.name.

Note : It is always good practice to use proper type like class , interface or typed object like tailor : {name: string, phone_number: number} to group object properties instead of using any

发布评论

评论列表(0)

  1. 暂无评论