0

Saving in the DB image file name, trying to send the file itself to the client.

here is my products array -

[
        {
            "id": "6115a592ff5cdc56e8b00bbd",
            "category": {
                "value": 1,
                "label": "Jeans"
            },
            "gender": {
                "value": 2,
                "label": "Women"
            },
            "title": "onosdc",
            "description": "vosdfjvnsod",
            "price": 223,
            "imageFileName": "1628808594497-8138.png"
        },
        {
            "id": "6115a5a5ff5cdc56e8b00bbe",
            "category": {
                "value": 1,
                "label": "Jeans"
            },
            "gender": {
                "value": 2,
                "label": "Women"
            },
            "title": "jdwoeid",
            "description": "jfwoeifjweoi",
            "price": 23902,
            "imageFileName": "1628808613195-7886.png"
        }
]

now i am mapping the imageFileName -

const imageFileNameArray = products.map((product) => {
        return product.imageFileName;
      });

now i got an array of the image file names -

[ '1628808594497-8138.png', '1628808613195-7886.png' ]

i got a folder with all the image files -

image files

this is how i am getting the image files array -

const directoryPath = path.join(__dirname, '../../images');
    fs.readdir(directoryPath, (e, images) => { }

i want to filter only the matching files, and send the matching files to client.

tried that -

const filterItems = (imageFileNameArray: string[], imageFileName: string) => {
        return imageFileNameArray.filter((el) => {
          return el.indexOf(imageFileName);
        });
      }

images.forEach((image) => {
        console.log(filterItems(imageFileNameArray, image));
      });

got this -

[ '1628808594497-8138.png', '1628808613195-7886.png' ]
[ '1628808594497-8138.png', '1628808613195-7886.png' ]
[ '1628808594497-8138.png', '1628808613195-7886.png' ]
[ '1628808594497-8138.png', '1628808613195-7886.png' ]
[ '1628808594497-8138.png', '1628808613195-7886.png' ]
[ '1628808594497-8138.png', '1628808613195-7886.png' ]
[ '1628808594497-8138.png', '1628808613195-7886.png' ]
[ '1628808594497-8138.png', '1628808613195-7886.png' ]
[ '1628808594497-8138.png', '1628808613195-7886.png' ]
[ '1628808594497-8138.png', '1628808613195-7886.png' ]
[ '1628808594497-8138.png', '1628808613195-7886.png' ]
[ '1628808594497-8138.png', '1628808613195-7886.png' ]
[ '1628808594497-8138.png', '1628808613195-7886.png' ]
[ '1628808594497-8138.png', '1628808613195-7886.png' ]
[ '1628808594497-8138.png', '1628808613195-7886.png' ]
[ '1628808613195-7886.png' ]
[ '1628808594497-8138.png' ]

i am not sure why i am getting all of this data, so what is left for my goal, is to filter between the arrays, and save the matching ones and send them to client.

the controller response -

res.status(200).send({
  success: true,
  message: "Successfully retrieved products",
  data: products.map((product) => ({
    id: product.id as string,
    category: {
      value: product.category,
      label: ServerGlobal.getInstance().getCategoryLabel(product.category)!,
    },
    gender: {
      value: product.gender,
      label: ServerGlobal.getInstance().getGenderLabel(product.gender)!,
    },
    title: product.title,
    description: product.description,
    price: product.price,
    imageFileName: product.imageFileName,
  })),
});
return;
oivwndvo
  • 3
  • 2

1 Answers1

0

You can filter two arrays like below...

const firstArray = [1, 2, 3, 4, 5, 8]
const secondArray = [8, 3]

//and now let's say we want to filter firstArray and get only the items that matched in secondArray.

const filteredArray = firstArray.filter(i => secondArray.includes(i))

console.log(filteredArray) // [3, 8]
Shamxeed
  • 382
  • 4
  • 8
  • thx! got another question. added the server ressponse to the Q. i need to compare the file name i get there to the files folder, you know how can i do that? – oivwndvo Aug 13 '21 at 00:24
  • I did not get your question, but do you want to send back the data you saved in the DB to the client? you can just do something like, ` const products = await product.save()` then `return res.status(200).json({products})` is that what you are looking for? – Shamxeed Aug 13 '21 at 00:40
  • no, i will post a new question – oivwndvo Aug 13 '21 at 00:52
  • Okay share the link please maybe I can be of help... – Shamxeed Aug 13 '21 at 00:59
  • https://stackoverflow.com/questions/68765726/send-a-file-from-server-after-filtering-the-file-from-a-db-file-name – oivwndvo Aug 13 '21 at 01:12