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