gpt4 book ai didi

javascript - 图像未上传到nodejs中的数据库

转载 作者:行者123 更新时间:2023-12-03 01:21:05 25 4
gpt4 key购买 nike

我想使用 put 方法编辑图像,但是当我给出路径时,它显示“文件名”未定义。图像更新,但当我跳到编辑图像时,它显示文件名未定义

app.put('/about_update/:id',upload.single('avatar'),(req,res,next)=>{
console.log(res.file);
aboutus.findById(req.params.id,(err,data2)=>{
if(err){
return res.send(err.message, '404 error from server');

}
data2.personName = req.body.personName;
data2.designation = req.body.designation;
data2.content = req.body.content;
data2.avatar = req.file.filename;
data2.save(function(err){
if(err){
res.send(err.message, 'err from server');
}else{
res.json({message:'updated successfully'});
}
})

})

})

错误:

事件.js:183 扔呃;//未处理的“错误”事件 ^

类型错误:无法读取未定义的属性“文件名” 在 aboutus.findById (/var/www/html/gis-react-version/api/server.js:147:33) 在/var/www/html/gis-react-version/api/node_modules/mongoose/lib/model.js:4451:16 在 process.nextTick (/var/www/html/gis-react-version/api/node_modules/mongoose/lib/query.js:2476:28)

最佳答案

    if (req.file) {
data.personName = req.body.personName;
data.designation = req.body.designation;
data.content = req.body.content;
data.avatar = req.file.filename;
}else{
data.personName = req.body.personName;
data.designation = req.body.designation;
data.content = req.body.content;
}
aboutus.findByIdAndUpdate(req.params.id, {
$set: {data}
}).then(() => {
res.json({message:'updated successfully'});
}, (err) => {
res.json({message:'err from server'});
})

关于javascript - 图像未上传到nodejs中的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51759743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com