javascript - 使用外键(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);
});
});
}
解决方案
推荐阅读
- rest - 我怎样才能用杰克逊只返回一个内部对象的几个 Json 字段
- mysql - Mysql按客户分组并获取最后3条记录
- swift - Firebase 云消息传递无法通过 API 运行
- javascript - CORS 问题 - Angular / PHP / Apache
- mysql - 查找与按字段排序的先前记录的差异
- php - yii2 queue proc_open 将第一个参数作为字符串除外,但给出了数组
- javascript - JS 功能在同一文件夹下的所有文件中可用
- javascript - mousedown 和 touchstart 未在移动设备上注册
- push-notification - 当应用程序被杀死时,推送通知不显示消息正文。它只显示应用程序的默认图标
- android - 带有 configChanges 的 Android DayNight 主题