Javascript 使用外键(id)获取角度中的值(名称)
请问,如何在另一个数据库中获取it Id表示的值? 我尝试使用map和subscribe,但如果您检查我的Javascript 使用外键(id)获取角度中的值(名称),javascript,angular,typescript,ionic-framework,Javascript,Angular,Typescript,Ionic Framework,请问,如何在另一个数据库中获取it Id表示的值? 我尝试使用map和subscribe,但如果您检查我的控制台。log我可以订阅数据,但它不能映射到数组对象。 管理user.component.ts public getUsers() { this.userService.getUsers().subscribe(data => { this.user = data['usrs']; const q = data['usrs'].map(x =>
控制台。log
我可以订阅数据,但它不能映射到数组对象。
管理user.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);
});
}
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'} ]}
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')
};
}
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);
});
});
}
控制台日志
(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
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);
});
}
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'} ]}
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')
};
}
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);
});
});
}
站点服务.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);
});
}
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'} ]}
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')
};
}
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);
});
});
}
数据共享。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);
});
}
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'} ]}
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')
};
}
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);
});
});
}
在console.log下,我有,staName:Object{closed:true,syncErrorSprown:false,syncErrorThrowable:true,…}
它假定为staName:Aloha Gas Ketu“
请共享此行的STANSERVICE代码staName:this.staService.getStation(x.stationId)这似乎是问题所在,请分离您的控制台日志以进行调试easily@NagaSaiA已经减少了console.log的内容,但我不明白您在这里的意思:请共享此行的StatService代码staName:this.StatService.getStation(x.stationId)StatService.ts中的getStation方法详细信息file@NagaSaiA已添加station.service.ts和data-sharing.service.ts