首页 > 解决方案 > 使用外键(id)以角度获取值(名称)

问题描述

请问,我怎样才能在另一个数据库中获得它 Id 所代表的值?我尝试使用 map 和 subscribe 但我认为缺少一些东西,如果你检查我的console.log我可以订阅数据但它不能订阅mapping数组对象。

管理用户.component.ts

  public getUsers() {
    this.userService.getUsers().subscribe(data => {
      this.user = data['usrs'];
      const q = data['usrs'].map(x => ({...x, staName: this.staService.getStation(x.stationId).subscribe((datum: any) => {
          console.log(datum['res']['name']);
          return{ staName: datum['res']['name']};
        })}))
      console.log(q);
    }, err => {
      alert(err.message);
    });
  }

控制台日志

(20) […]
​
0: {…}
​​
authorities: Array [ {…} ]
​​
id: 1
​​
img: "assets/images/Pictures/faces-images/face_image1.png"
​​
name: "Temidayo Abiodun"
​​
orgId: 1
​​
password: "banjo101"
​​
role: "attendant"
​​
staName: Object { closed: true, syncErrorThrown: false, syncErrorThrowable: true, … }
​​
stationId: 1
​​
username: "attendant1@wamiri.com"

常数.ts

export const usrs: User[] = [
    { id: 1,  orgId: 1,   stationId: 1,   username: 'attendant1@wamiri.com',  password: 'banjo101',   name: 'Temidayo Abiodun',     img: 'assets/images/Pictures/faces-images/face_image1.png', role: Role.Attendant, authorities: [{authority: 'ROLE_ATTENDANT'} ]},
    { id: 2,  orgId: 1,   stationId: 1,   username: 'attendant2@wamiri.com',  password: 'banjo102',   name: 'Oluwayemisi Adebayo',  img: 'assets/images/Pictures/faces-images/face_image2.png', role: Role.Attendant, authorities: [{authority: 'ROLE_ATTENDANT' }] },
    { id: 3,  orgId: 1,   stationId: 1,   username: 'attendant3@wamiri.com',  password: 'banjo103',   name: 'Oluwole Afolabi',      img: 'assets/images/Pictures/faces-images/face_image3.png', role: Role.Attendant, authorities: [{authority: 'ROLE_ATTENDANT' }] },
    { id: 4,  orgId: 1,   stationId: 1,   username: 'attendant4@wamiri.com',  password: 'banjo104',   name: 'Oluwafemi Afolayan',   img: 'assets/images/Pictures/faces-images/face_image4.png', role: Role.Attendant, authorities: [{authority: 'ROLE_ATTENDANT' }] },
    { id: 5,  orgId: 2,   stationId: 5,   username: 'attendant5@wamiri.com',  password: 'banjo105',   name: 'Adebola Ayodele',      img: 'assets/images/Pictures/faces-images/face_image5.png', role: Role.Attendant, authorities: [{authority: 'ROLE_ATTENDANT'} ]}

站服务.ts

import { Injectable } from '@angular/core';
import { Station } from '../_models/station';
import { DataSharingService } from './data-sharing.service';

@Injectable({
  providedIn: 'root'
})
export class StationService {
  sta: Station;

  constructor(
    private dataSharing: DataSharingService,
  ) { }

  getStations() {
    return this.dataSharing.httpGetAll('station');
  }

  getStation(id: any) {
    return this.dataSharing.httpGetOne(id, 'station');
  }

  getStationsByOrg(id) {
    return this.dataSharing.httpGetAllBy(id, 'orgId', 'station')
    };
}

数据共享.ts

  public httpGetAll(owner: any) {
    return this.http.get(`${this.url}/${owner}`, this.httpOptions).pipe(
      catchError(e => {
        throw new Error(e);
      })
    );
  }

  public httpGetOne(id: number, owner: any) {
    return this.http.get(`${this.url}/${owner}/${id}`, this.httpOptions).pipe(
      catchError(e => {
        throw new Error(e + ' Please try again later.');
      })
    );
  }

  public httpGetAllBy(id: number, byId:string, owner: any) {
      return new Promise((resolve, reject) => {
       this.httpGetAll(owner)
        .subscribe(data => {
          let filterdData = Object.keys(data).map(function(key) {
           return data[key].filter(x => x[byId] == id);
          });
          resolve(filterdData);
        }, err => {
          console.log(err.message);
        });
      });
  }

我希望它显示名称,而不是 id

标签: javascriptangulartypescriptionic-framework

解决方案


推荐阅读