【问题标题】:error TS2339: Property 'name' does not exist on type 'never'错误 TS2339:“从不”类型上不存在属性“名称”
【发布时间】:2021-06-21 02:33:56
【问题描述】:

app.component.html 如您所见,我正在尝试通过 *ngFor

显示我的 firebase 项目的响应数据
<div class="row">
        <div class="col-md-3">
            <h4 class="text-warning">All Employee Data</h4>
            <div class="card" *ngFor="let item of employee">
                <div class="card-body">
                    <div class="card-title">
                        Name: {{item.name}} // error-Property 'name' does not exist on type 'never'.
                    </div>
                    <div class="card-subtitle">
                        Age: {{item.age}} // error-Property 'name' does not exist on type 'never'.
                    </div>
                    <div class="card-text">
                        Address: {{item.address}} // same error here also
                    </div>
                </div>
            </div>
        </div>
    </div>

app.component.ts 如您所见,我正在尝试从 firesbase 项目中获取数据。

export class AppComponent implements OnInit {
  title = 'App';

 constructor( private _crudService:CrudService) {}

     employee:[] = [];
     employeeName: string = '';
     employeeAge: number | undefined;
     employeeAddress: string = '';
     message: string ='';

 ngOnInit(){
  this._crudService.getAllEmployee().subscribe((data:any) =>{
    this.employee = data.map((e:any) =>{   // here I'm storing res in empty array  
      return{
        id: e.payload.doc.id,
        name: e.payload.doc.data()['name'],
        age: e.payload.doc.data()['age'],
        address: e.payload.doc.data()['address']

      }
    });
    console.log(this.employee);
  }); 
 }

crud.service.ts 如您所见,我正在发送获取员工数据的请求

getAllEmployee(){
    return this.fireservice.collection('Employee').snapshotChanges();
  }

【问题讨论】:

  • 为什么你不在 ngOnInit 中使用 e.payload.doc.data.name 而不是 e.payload.doc.data()['name']
  • 让我试试..如果它有效,提前谢谢你
  • 不,伙计,它不起作用
  • 您的代码是正确的,您可以检查来自 _crudService.getAllEmployee 的响应,或者您可以调试 `this.employee = data.map((e:any) =>{ return{ id: e.payload.doc.id,名称:e.payload.doc.data()['name'],年龄:e.payload.doc.data()['age'],地址:e.payload.doc。数据()['地址'] } }); `
  • 是的,响应没有问题,我收到这样的响应。地址:“艾哈迈达巴德”年龄:21 id:“xS0Uz0eXCNyb4YciVadj”名称:“harsh”

标签: angular typescript firebase google-cloud-firestore


【解决方案1】:

选项 1:安全导航运算符

最初渲染组件时,数组employee 为空。您可以使用安全导航运算符?. 在尝试呈现之前检查属性是否可用。

<div class="card-title">
  Name: {{ item?.name }}
</div>
<div class="card-subtitle">
  Age: {{ item?.age }}
</div>
<div class="card-text">
  Address: {{ item?.address }}
</div>

选项 2:async 管道

如果变量this.employee仅用于在模板中呈现输出,您可以跳过控制器中的订阅,并在模板中使用async管道。

控制器

import { Observable } from 'rxjs';
import { map, tap } from 'rxjs/operators';

export class AppComponent implements OnInit {
  title = 'App';
  employees$: Observable<Array<any>>;        // <-- type observable here
  employeeName: string = '';
  employeeAge: number | undefined;
  employeeAddress: string = '';
  message: string = '';

  constructor(private _crudService: CrudService) {}

  ngOnInit() {
    this.employees$ = this._crudService.getAllEmployee().pipe(
      map((e: any) => ({
        id: e.payload.doc.id,
        name: e.payload.doc.data()['name'],
        age: e.payload.doc.data()['age'],
        address: e.payload.doc.data()['address']
      })),
      tap(console.log) // <-- check the value
    );
  }
}

模板

<ng-container *ngIf="(employees$ | async) as employees">
  <div class="row">
    <div class="col-md-3">
      <h4 class="text-warning">All Employee Data</h4>
      <div class="card" *ngFor="let item of employees">
        <div class="card-body">
          <div class="card-title">
            Name: {{ item['name'] }}     <!-- bracket notation instead of dot notation -->
          </div>
          <div class="card-subtitle">
            Age: {{ item['age'] }}
          </div>
          <div class="card-text">
            Address: {{ item['address'] }}
          </div>
        </div>
      </div>
    </div>
  </div>
</ng-container>

【讨论】:

  • 不,它不起作用,在导航操作符后抛出同样的错误?。
  • 我收到类似地址的回复:“Ahmedab​​ad”年龄:21 id:“xS0Uz0eXCNyb4YciVadj”名称:“harsh”
  • @harsh:即使出现错误,输出也会呈现吗?
  • 没有输出不呈现。编译时抛出错误
  • 听起来不是 Linter 问题。但是,您可以尝试使用括号表示法而不是点表示法。我已经更新了另一种获得结果的方法的答案。请看看它是否适合你。
【解决方案2】:

之前可以使用JSON.parse(JSON.stringify(data)) this.employee = data.map((e:any) =&gt;{ // here I'm storing res in empty array

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-13
    • 1970-01-01
    • 2019-01-21
    • 2016-08-13
    • 2017-12-20
    相关资源
    最近更新 更多