0

目前,我正在开发一种使用 Express Router 将消息(文件和字段)从 Dropzone 上传到 Mongoose 的方法。我的后端控制器(在身份验证和数据验证后调用)如下:

//Import Internal Dependencies
const Loader = require('../models/loader.js');
const Formidable = require('formidable');
const fs = require('fs');

module.exports = {
    load: async (req, res, next) => {

        var form = new Formidable.IncomingForm();

        let path;
        let contentType;

        await form.parse(req, async function (err, fields, files) {

            if (err) {

                return res.status(404).json(err);

            } else {

                const {
                    user,
                    patient,
                    condition,
                    compound,
                    classi
                } = fields;

                path = files.image.path;
                contentType = files.image.type;
                fs.readFile(path, async function (err, data) {

                    if (err) {

                        return res.status(404).json(err);

                    } else {

                        //Save load
                        const newLoader = new Loader({
                            user,
                            patient,
                            condition,
                            compound,
                            classi,
                            image: {
                                data,
                                contentType
                            }
                        });

                        //Delete image in local storage
                        await fs.unlink(path, function (error) {
                            if(error){
                            return res.status(404).json(error)
                            }
                        });

                        await newLoader.save();

                        res.status(200).json("Load image sucessfully.");

                        next()
                    }
                })
            }
        });
    }
};

当我用 Postman 测试它时,我得到了状态 202,并且图像已成功上传到数据库。但是,当我尝试使用不带字段的 dropzone 上传时(这会导致一些错误并显示在 dropzone 中)我在后端控制台中收到以下错误/警告(Dropzone 在上传时停止并且没有显示任何错误) :

(node:20834) UnhandledPromiseRejectionWarning: ValidationError: load validation failed: user: Path `user` is required., classi: Path `classi` is required.
    at new ValidationError (/root/aimuneBack/node_modules/mongoose/lib/error/validation.js:27:11)
    at model.Document.invalidate (/root/aimuneBack/node_modules/mongoose/lib/document.js:1876:32)
    at p.doValidate.skipSchemaValidators (/root/aimuneBack/node_modules/mongoose/lib/document.js:1744:17)
    at /root/aimuneBack/node_modules/mongoose/lib/schematype.js:808:9
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)
(node:20834) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:20834) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
    at emitWarning (internal/process/promises.js:92:15)
    at emitPendingUnhandledRejections (internal/process/promises.js:109:11)
    at process._tickCallback (internal/process/next_tick.js:189:7)
POST /load - - ms - -

所以我知道我的异步代码做错了,不幸的是无法弄清楚。希望你能帮忙。最好的问候,安德烈

4

0 回答 0