0

我想从服务器发送一个文件,我将文件名保存在数据库中。在我用产品文件名过滤文件后,我想发送文件。

这是我的过滤 -

  try {
    const products = await ProductDB.find({
      category: +req.query.category!,
      gender: +req.query.gender!,
    });
   
    // Getting image files
    const directoryPath = path.join(__dirname, '../../images');

    fs.readdir(directoryPath, (e, images) => {
      if (e) { 
        ServerGlobal.getInstance().logger.error(`<getProducts>: 'Unable to scan directory: ' + ${e}`) 
      } 

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

      const filteredImageFilesArray = images.filter(i => imageFileNameArray.includes(i))
    });

这是服务器响应 -

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

我想product.imageFileName用相等的文件替换,如何用文件过滤文件名并从服务器发送。

4

0 回答 0