我不知道这是否可能,但我试图使用placePlaceId
外键将每个图像数组附加到它们的帖子中。因此,在使用place_id:3
的第一个对象中,我希望使用外键placePlaceId:3
附加所有图像。
,这就是我如何实现我的目标,
async getPlaces() {
let imagesList: Images[] = [];
const places = await this.repo.find();
const imageQuery = await this.imagesRepo
.createQueryBuilder()
.select('*')
.getRawMany();
places.forEach((place, index: number) => {
for(let i = 0; i < imageQuery.length; i++){
place.place_id === imageQuery[i].placePlaceId
? imagesList.push(imageQuery[i])
: 0;
}
place.images = imagesList;
this.repo.save(place);
console.log(place.place_id, imageQuery);
});
console.log(imagesList, 'imagesList');
return places;
}
电流结果
[
{
"place_id": 3,
"title": "البيك",
"description": "الذ مطعم سعودي",
"signature": "المسحب و البروست",
"isFavorite": false,
"approved": false,
"phone": null,
"website": null,
"instagram": null,
"Sunday": "4-11",
"Monday": "4-11",
"Tuesday": "4-11",
"Wednesday": "4-11",
"Thursday": "4-11",
"Friday": "4-11",
"Saturday": "4-11",
"images": [
{
"image_id": 4,
"image": "http://eatnstays.com/Arabic/wp-content/uploads/2021/06/20180216_134034-12.jpg",
"image_owner": "google",
"placePlaceId": 3
},
{
"image_id": 5,
"image": "http://eatnstays.com/Arabic/wp-content/uploads/2021/06/20180216_134034-12.jpg",
"image_owner": "google",
"placePlaceId": 3
},
{
"image_id": 6,
"image": "https://i.ytimg.com/vi/Fak54PuiW9g/maxresdefault.jpg",
"image_owner": "google",
"placePlaceId": 5
},
{
"image_id": 7,
"image": "https://i.ytimg.com/vi/Fak54PuiW9g/maxresdefault.jpg",
"image_owner": "google",
"placePlaceId": 5
},
],
{
"place_id": 5,
"title": "اثراء",
"description": "مركز الملك عبدالعزيز العالمي",
"signature": "شامل",
"isFavorite": false,
"approved": false,
"phone": null,
"website": null,
"instagram": null,
"Sunday": "4-11",
"Monday": "4-11",
"Tuesday": "4-11",
"Wednesday": "4-11",
"Thursday": "4-11",
"Friday": "4-11",
"Saturday": "4-11",
"images": [
{
"image_id": 4,
"image": "http://eatnstays.com/Arabic/wp-content/uploads/2021/06/20180216_134034-12.jpg",
"image_owner": "google",
"placePlaceId": 3
},
{
"image_id": 5,
"image": "http://eatnstays.com/Arabic/wp-content/uploads/2021/06/20180216_134034-12.jpg",
"image_owner": "google",
"placePlaceId": 3
},
{
"image_id": 6,
"image": "https://i.ytimg.com/vi/Fak54PuiW9g/maxresdefault.jpg",
"image_owner": "google",
"placePlaceId": 5
},
{
"image_id": 7,
"image": "https://i.ytimg.com/vi/Fak54PuiW9g/maxresdefault.jpg",
"image_owner": "google",
"placePlaceId": 5
},
]
]
预期结果
[
{
"place_id": 3,
"title": "البيك",
"description": "الذ مطعم سعودي",
"signature": "المسحب و البروست",
"isFavorite": false,
"approved": false,
"phone": null,
"website": null,
"instagram": null,
"Sunday": "4-11",
"Monday": "4-11",
"Tuesday": "4-11",
"Wednesday": "4-11",
"Thursday": "4-11",
"Friday": "4-11",
"Saturday": "4-11",
"images": [
{
"image_id": 4,
"image": "http://eatnstays.com/Arabic/wp-content/uploads/2021/06/20180216_134034-12.jpg",
"image_owner": "google",
"placePlaceId": 3
},
{
"image_id": 5,
"image": "http://eatnstays.com/Arabic/wp-content/uploads/2021/06/20180216_134034-12.jpg",
"image_owner": "google",
"placePlaceId": 3
},
],
{
"place_id": 5,
"title": "اثراء",
"description": "مركز الملك عبدالعزيز العالمي",
"signature": "شامل",
"isFavorite": false,
"approved": false,
"phone": null,
"website": null,
"instagram": null,
"Sunday": "4-11",
"Monday": "4-11",
"Tuesday": "4-11",
"Wednesday": "4-11",
"Thursday": "4-11",
"Friday": "4-11",
"Saturday": "4-11",
"images": [
{
"image_id": 6,
"image": "https://i.ytimg.com/vi/Fak54PuiW9g/maxresdefault.jpg",
"image_owner": "google",
"placePlaceId": 5
},
{
"image_id": 7,
"image": "https://i.ytimg.com/vi/Fak54PuiW9g/maxresdefault.jpg",
"image_owner": "google",
"placePlaceId": 5
},
]
]
发布于 2022-03-23 09:28:19
首先,看上去你每次都在抓取你所有的位置和图像。这可能是一个非常糟糕的想法,如果地方和/或图像的数量变得非常多,会给你带来很大的痛苦。
尽管如此,代码的问题是,您只创建一个imagesList
并重复使用它,然后将它附加到所有place
上。
一个小的解决方法是将imagesList
的创建移动到places.forEach
迭代中:
places.forEach((place, index: number) => {
let imagesList: Images[] = [];
for(let i = 0; i < imageQuery.length; i++){
place.place_id === imageQuery[i].placePlaceId
? imagesList.push(imageQuery[i])
: 0;
}
place.images = imagesList;
this.repo.save(place);
console.log(place.place_id, imageQuery);
});
https://stackoverflow.com/questions/71584438
复制相似问题