getting result from another middleware returns undefined

  javascript, middleware, node.js, promise, return

i have an upload middlware that store the names of uploaded images in images[] and must return it

 exports.uploadImages = async (req,res) => {
     .......

     UPLOAD_HELPERS.uploadMultipleImages(req, res, (err) => {
      if (err instanceof multer.MulterError) {
        if (err.code === "LIMIT_UNEXPECTED_FILE") {
          return res.send("Too many files to upload / Check files type.");
        }
      } else if (err) {
        return res.send(err);
      }

      Promise.all(
        req.files.map(async file => {
         newFileName = `${crypto.createHash('sha256').update(Date.now().toString()).digest('hex')}.jpeg`;       
          
          await sharp(file.buffer)
            .toFormat("jpeg")
            .jpeg({ quality: 80 })
            .toFile(path.join(dir, newFileName));
            images.push(newFileName);

        })

      ).then( values => {
        if (images.length <= 0) {
          return res.send(`You must select at least 1 image.`);
        }
 
          return res.send({done : true,
                            images : images,
                            imageType : imageType});
  }
      )
      

in an another controller i need to get the images[] from the first middlware, i did :

exports.createPosts = async (req, res) => {
.......
    // *************************** problem getting results here **********************
    const uploaded = await UPLOAD.uploadImages(req,res).then((result) => console.log(result.images))
    
..........
  }

console.log(result.images) : return undefined

also any suggestion to improve the code is welcomed

Source: Ask Javascript Questions

LEAVE A COMMENT