如何将图像数组附加到 Post 对象

How to attach an array of images to a Post object

我不知道这是否可能,但我正在尝试使用 placePlaceId 外键将每个图像数组附加到它们的 post。所以在 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
            },
             ]
           ]

首先,您似乎每次都在获取所有地点和所有图像。这可能是一个非常糟糕的主意,如果 and/or 图片的位置数量变得非常大,会给你带来很多痛苦。

就是说,您的代码的问题在于您只创建了一个 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);
});