简体   繁体   English

Angular 7地图运算符无法与httpClient Observable一起使用

[英]Angular 7 map operator is not working with httpClient Observable

I got "undefined" after adding the map operator 添加地图运算符后出现“未定义”

Angular version: 7 角度版本:7

Map import: 地图导入:

import { map } from 'rxjs/operators';

With Map: 使用地图:

this.restaurantService.getRestaurants().pipe(map((restaurant:any) => restaurant.name)).subscribe((restaurants) => {
  console.log(restaurants); // undefined
});

Not necessary "name" field is returning undefined at any field 不必要的“名称”字段在任何字段都返回未定义

Without Map 没有地图

this.restaurantService.getRestaurants().subscribe((restaurants) => {
  console.log(restaurants); // [{...,name: 'lorem'},{...},...]
});

Service: 服务:

  getRestaurants(): Observable<_Restaurant[]> {
    return this.get(api.restaurants) as Observable<_Restaurant[]>;
  };

I logged the restaurant inside the map operator and I got an array which is should be object 我在地图运算符中记录了餐厅,并得到了一个应该是对象的数组

 .pipe(map(restaurant => {
    console.log(restaurant); //[{},{}] !!
    return restaurant.name;
  }))

Any Idea why I'm getting the undefined? 知道为什么我会得到不确定的信息吗?

this.restaurantService
    .getRestaurants()
    .pipe(map((restaurants:any[]) => restaurants.map(restaurant => restaurant.name)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM