How to save an array of files to mongoDB database using multer?

梦想的初衷 提交于 2019-12-13 03:18:53

问题


I'm creating an endpoint using nodejs which will store an array of images using multer. It is allowing me to save a single image, but have no idea how to proceed for multiple images. For uploading single image this is the code I've written:

router.post ('/upload',auth, upload.single('upload'), async (req:Request, res:Response) =>{
    const bufferData = {
        image:req.file.buffer
    };

    const image = new Image(bufferData);    
    image.save();
    res.send()
},(error:Error, req:Request, res:Response, next:NextFunction)=>{    
    res.status(400).send({error:error.message})
})

For single file upload this is working fine. For multiple files upload I'm trying this code.

router.post ('/upload',auth, upload.array('upload'), async (req:Request, res:Response) =>{
    const bufferData = {
        images:req.files         // no here option for req.files.buffer
    };

        const image = new Image(bufferData);    
        image.save();
        res.send()
    },(error:Error, req:Request, res:Response, next:NextFunction)=>{    
        res.status(400).send({error:error.message})
    })

The type of data to store the image needs to be a Buffer. But there is no any option to get the array of images in buffer format, like req.file.buffer. What is the way to et this data in Array format?


回答1:


Found one trick to do it.

const files = req.files;
const images = map(files, (file:any)=>{
    const bufferData = (file as Express.Multer.File).buffer;
    return bufferData;
})

This 'images' is now an array of Buffer type, which you can store to mongoDB database as binary files.




回答2:


Without seeing your full code, I am assuming your Image class is a class that extends mongoose which has some sort of save method?

If this is the case, your save() method is probably asynchronous. This means, you need to wait for the method to resolve before continuing / returning.

router.post ('/upload',auth, upload.array('upload'), async (req:Request, res:Response) => {
    const promises = req.files.map(file => {
        const image = new Image(file.buffer) // create the new Image
        return image.save // return the promise without calling it yet
    })

    const images = await Promise.all(promises) // calls all the porimises returned in `promises`

    // we let the Promise.all resolve before calling res.send()

    res.send(images) 
},(error:Error, req:Request, res:Response, next:NextFunction)=>{    
    res.status(400).send({error:error.message})
})

As you can see here, we create an array of promises called promises.

We then call all of these with Promise.all(promises) and once they have all resolved, we can then return with our res.send method

I haven't tested this code and I have had to take a bit of a guess as to what the rest of your code base looks like so hopefully this works.



来源:https://stackoverflow.com/questions/56093124/how-to-save-an-array-of-files-to-mongodb-database-using-multer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!