【问题标题】:MEAN app error: not able to get response from get request平均应用程序错误:无法从获取请求中获得响应
【发布时间】:2021-08-07 13:56:00
【问题描述】:

获取

它发出一个 get req 但没有返回任何东西。甚至没有任何错误它只是一次又一次地发出 get req。

app.get('/:shortUrl',async (req,res)=>{
   try{ 
    const shortUrl = await shorturl.findOne({ short: req.params.shortUrl })
   .then(()=>{
      if(shortUrl == null) return res.sendStatus(404);
      res.redirect(shortUrl.full);
   })
  }
  catch{(error)=> console.log(error)};
})

【问题讨论】:

    标签: node.js angular mongodb express


    【解决方案1】:

    问题是您同时使用await.then

    使用 await

    app.get('/:shortUrl',async (req,res)=>{
       try{ 
          const shortUrl = await shorturl.findOne({ short: req.params.shortUrl })
          if(!shortUrl) return res.sendStatus(404);
          res.redirect(shortUrl.full);
       } catch (error) {console.log(error)};
    })
    

    或使用 .then

    app.get('/:shortUrl',(req,res)=>{
            shorturl.findOne({ short: req.params.shortUrl })
            .then(shortUrl => {
               if(!shortUrl) return res.sendStatus(404);
               res.redirect(shortUrl.full);
            })
            .catch(error => {console.log(error)};
        })
    

    【讨论】:

      猜你喜欢
      • 2015-07-08
      • 1970-01-01
      • 2022-01-08
      • 2021-02-07
      • 2015-02-06
      • 2017-06-22
      • 1970-01-01
      • 2020-04-20
      • 1970-01-01
      相关资源
      最近更新 更多