【问题标题】: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)};
})