Where should i put async and await? result is work2 work1
app.post('/upload', (req, res) => {
const txtupload = multer().any()
let validate = true
txtupload(req,res, (err)=>{
console.log('work1')
validate = false
})
if(validate){
console.log('work2')
//code
}
});
The upload function from multer
doesn't return a promise, so async
/ await
isn't applicable. Instead, issue your response in the callback.
Your real question seems to be: How do I use multer
to handle file uploads? For that we go to the multer
documentation . Adapting that documentation to your code:
app.post('/upload', (req, res) => {
const txtupload = multer().any();
txtupload(req,res, (err) => {
if (err) {
// ...it failed, send a failure response via `res`...
} else {
// ...it worked, send a success response via `res`...
}
});
// Don't do anything here, the upload hasn't been processed yet
});
But refer to the examples in the documentation, there are other patterns for using multer
.
因为它是一个箭头函数,所以你必须把它放在参数之前:
app.post('/upload', async (req, res) => { ... }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.