Express中用于对Mongoose进行表单解析的异步控制器
目前,我正在开发一种使用Express Router将消息(文件和字段)从Dropzone上传到Mongoose的方法。我的后端控制器(在身份验证和数据验证后调用)如下所示:Express中用于对Mongoose进行表单解析的异步控制器,express,mongoose,vue.js,formidable,dropzone,Express,Mongoose,Vue.js,Formidable,Dropzone,目前,我正在开发一种使用Express Router将消息(文件和字段)从Dropzone上传到Mongoose的方法。我的后端控制器(在身份验证和数据验证后调用)如下所示: //Import Internal Dependencies const Loader = require('../models/loader.js'); const Formidable = require('formidable'); const fs = require('fs'); module.exports
//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()
}
})
}
});
}
};
当我用邮递员测试它时,我得到了状态202,图像成功上传到数据库。但是,当我尝试在没有字段的情况下使用dropzone上载时(这会导致一些错误并显示在dropzone中),我在后端控制台中收到以下错误/警告(dropzone在上载时停止,并且没有显示任何错误):
因此,我知道我的异步代码有问题,但不幸的是,我无法解决这个问题。希望你能帮忙。向您致意,Andre这段代码中有很多错误。对于初学者来说,
await
仅当您正在等待的东西是一个承诺时才等待,而该承诺只有在异步操作完成时才被正确解析或拒绝。您正在使用wait
处理一些不返回承诺的内容。不要将承诺与普通异步回调混合使用。选择一个模型或另一个模型。不要同时使用这两种方法(最好是承诺所有异步函数,并对所有控制流使用承诺)。
(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 - -