Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Angular 7找不到不同的支撑对象';[对象对象]';类型为';对象';。NgFor只支持绑定到数组之类的可重用文件_Javascript_Angular_Typescript_Angular7_Angular Template - Fatal编程技术网

Javascript Angular 7找不到不同的支撑对象';[对象对象]';类型为';对象';。NgFor只支持绑定到数组之类的可重用文件

Javascript Angular 7找不到不同的支撑对象';[对象对象]';类型为';对象';。NgFor只支持绑定到数组之类的可重用文件,javascript,angular,typescript,angular7,angular-template,Javascript,Angular,Typescript,Angular7,Angular Template,从数据库检索数据时出错 找不到类型为“object”的不同支持对象“[object]”。NgFor只支持绑定到数组之类的可重用文件 我尝试了所有剩余的回答,但没有得到确切的答案 我的服务。ts: users: User[]; getAllUsers(){ return this.http.get(environment.apiBaseUrl + '/users'); } refreshUserList(){ this.userService.getAllUser

从数据库检索数据时出错

找不到类型为“object”的不同支持对象“[object]”。NgFor只支持绑定到数组之类的可重用文件

我尝试了所有剩余的回答,但没有得到确切的答案

我的服务。ts

  users: User[];
  getAllUsers(){
    return this.http.get(environment.apiBaseUrl + '/users');
  }
  refreshUserList(){
    this.userService.getAllUsers().subscribe((res) => {
      this.userService.users = res as User[];
    })
  };
<tr *ngFor="let use of userService.users">
  <td>{{ use.fullName }}</td>
</tr>
users: User[];

refreshUserList(){
    this.userService.getAllUsers().subscribe(res => {
      res !== null : this.docs = res.docs;
    })
};
<tr *ngFor="let user of users?.docs">
  <td>{{ user.fullName }}</td>
</tr>
我的组件。ts

  users: User[];
  getAllUsers(){
    return this.http.get(environment.apiBaseUrl + '/users');
  }
  refreshUserList(){
    this.userService.getAllUsers().subscribe((res) => {
      this.userService.users = res as User[];
    })
  };
<tr *ngFor="let use of userService.users">
  <td>{{ use.fullName }}</td>
</tr>
users: User[];

refreshUserList(){
    this.userService.getAllUsers().subscribe(res => {
      res !== null : this.docs = res.docs;
    })
};
<tr *ngFor="let user of users?.docs">
  <td>{{ user.fullName }}</td>
</tr>
mycomponent.html

  users: User[];
  getAllUsers(){
    return this.http.get(environment.apiBaseUrl + '/users');
  }
  refreshUserList(){
    this.userService.getAllUsers().subscribe((res) => {
      this.userService.users = res as User[];
    })
  };
<tr *ngFor="let use of userService.users">
  <td>{{ use.fullName }}</td>
</tr>
users: User[];

refreshUserList(){
    this.userService.getAllUsers().subscribe(res => {
      res !== null : this.docs = res.docs;
    })
};
<tr *ngFor="let user of users?.docs">
  <td>{{ user.fullName }}</td>
</tr>

{{use.fullName}

因此,请使用以下命令,因为您可以看到响应不是数组,而是其中一个字段是数组。因此,必须迭代数组字段

*ngFor="let use of userService.users?.docs"

您应该将用户存储在组件中的
User
本地数组中,并在
users.docs

组件。ts

  users: User[];
  getAllUsers(){
    return this.http.get(environment.apiBaseUrl + '/users');
  }
  refreshUserList(){
    this.userService.getAllUsers().subscribe((res) => {
      this.userService.users = res as User[];
    })
  };
<tr *ngFor="let use of userService.users">
  <td>{{ use.fullName }}</td>
</tr>
users: User[];

refreshUserList(){
    this.userService.getAllUsers().subscribe(res => {
      res !== null : this.docs = res.docs;
    })
};
<tr *ngFor="let user of users?.docs">
  <td>{{ user.fullName }}</td>
</tr>
component.html

  users: User[];
  getAllUsers(){
    return this.http.get(environment.apiBaseUrl + '/users');
  }
  refreshUserList(){
    this.userService.getAllUsers().subscribe((res) => {
      this.userService.users = res as User[];
    })
  };
<tr *ngFor="let use of userService.users">
  <td>{{ use.fullName }}</td>
</tr>
users: User[];

refreshUserList(){
    this.userService.getAllUsers().subscribe(res => {
      res !== null : this.docs = res.docs;
    })
};
<tr *ngFor="let user of users?.docs">
  <td>{{ user.fullName }}</td>
</tr>

{{user.fullName}

另一方面,您可以通过以下方式检查数据是否已准备好在html文件中提供

<div *ngIf="userService.users?.length > 0">
   <tr *ngFor="let use of userService.users">
       <td>{{ use.fullName }}</td>
    </tr>
</div>

{{use.fullName}

你能把响应记录下来吗?所以你做了一些奇怪的事情。您的响应不是用户[]的类型。尝试在getAllUsers()方法(用户[])中添加响应类型。并在http.get中添加this.http.get(environment.apiBaseUrl+'/users');{status:true,docs:Array(5)}这是我的console.log响应@dileepkumarjamitry
*ngFor=“let use of userService.users?.docs”
,因为您根据console.log在模板中引用了错误的对象。谢谢@ForestG它正在工作。。