Home > Mobile >  How to attach an array of images to a Post object
How to attach an array of images to a Post object

Time:03-23

I don't know if it's possible or not, but I'm trying to attach each array of images to their post using the placePlaceId foreign key. So in the first object with place_id:3 I want to attach all the images with the foreign key placePlaceId:3.

this is how I'm trying to achieve my goal

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;
  }

Current Resulut

    [
{
    "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
        },
         ]
       ]

Expected Result

[
    {
        "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
            },
             ]
           ]

CodePudding user response:

Firstly it looks like you're fetching all your places and all your images every time. That's probably a Very Bad Idea and will cause you a lot of pain if the number of places and/or images gets very large.

That said, the problem with your code is that you are only creating a single imagesList and using it repeatedly, then attaching it to all your places.

A small fix would be to move the creation of imagesList into your places.forEach iteration:

 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);
});
  • Related