最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

异步功能不起作用,无法更新和上传图像

网站源码admin16浏览0评论

异步功能不起作用,无法更新和上传图像

异步功能不起作用,无法更新和上传图像

我正在使用一个功能,其中有页面要求用户提供个人资料图片和一些信息。到目前为止,这就是我所拥有的

router.post('/edit/:id', async function(req, res) {
  let user
  const id = req.params.id
  try{
    user = await User.findById(id)
    user.firstname= req.user.firstname,
    user.lastname= req.user.lastname,
    user.religion= req.user.religion,
    user.education= req.user.education,
    user.language= req.user.language,
    user.lookingfor=req.body.lookingfor,
    user.preferEdu=req.user.preferEdu,
    user.preferReligion= req.user.preferReligion,
    user.bio= req.user.bio
    if (req.file != null && req.file !== '') {
      await upload(req, res, (err) => {
        if(err){
          console.log(err)
         } else {
           if(req.file == undefined){
             console.log('no file')
           } else {
             console.log(req.file)
            User.findByIdAndUpdate({_id: id}, {$set: {
               imageurl: req.file.location
             }}, {new: true}, (err, result) => {
               res.redirect('/dashboard')
             })
           }
         }
      })
    }

  } catch{
    if (user != null) {
      console.log ('no user')
    } else {
      res.redirect('/dashboard')
    }

  }

})

页面只是跳回到'/ dashboard',没有任何保存

在我有一个仅能处理上传图像的功能之前,这是成功的

router.post('/edit/:id', function(req, res) {
  const id = req.params.id
    upload(req, res, (err) => {
    if(err){
     console.log(err)
    } else {
      if(req.file == undefined){
        console.log('no file')
      } else {
        console.log(req.file)
       User.findByIdAndUpdate({_id: id}, {$set: {
          imageurl: req.file.location
        }}, {new: true}, (err, result) => {
          res.redirect('/dashboard')
        })


      }
    }
  });
});

我可以看到图像已上传到Amazon s3,并且req.file.location已记录在控制台中。但它没有保存在我的mongo数据库中。任何帮助将不胜感激!谢谢

回答如下:

您必须使用位置的大写字母“ l”。您应该将行req.file.location切换到req.file.Location。 S3返回带有带有大写字母作为首字母的键的json。

希望,这会有所帮助!让我知道是否可行!

发布评论

评论列表(0)

  1. 暂无评论