如何使用multer将文件数组保存到mongoDB数据库

如何使用multer将文件数组保存到mongoDB数据库

本文介绍了如何使用multer将文件数组保存到mongoDB数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在使用nodejs创建一个端点,该端点将使用multer存储一系列图像.它允许我保存单个图像,但不知道如何继续处理多个图像.为了上传单个图像,这是我编写的代码:

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

用于存储图像的数据类型必须为Buffer.但是没有任何选择来获取缓冲区格式的图像数组,例如req.file.buffer.用数组格式处理这些数据的方法是什么?

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?

推荐答案

我没有看到完整的代码,我假设您的Image类是扩展mongoose的类,具有某种保存方法?

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

如果是这种情况,则您的save()方法可能是异步的.这意味着,您需要等待方法解析后才能继续/返回.

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

正如您在此处看到的,我们创建了一个称为promises的诺言数组.

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

然后用Promise.all(promises)调用所有这些,一旦它们全部解决,就可以使用我们的res.send方法返回

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.

这篇关于如何使用multer将文件数组保存到mongoDB数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 16:14