为什么我无法在Javascript中获取JSON响应数据?使用axios+express



我正试图获得歌词作为对这个简单歌词请求的回应(例如Eminem的一首歌(,它没有给我任何错误,但我不明白为什么我没有得到回应我错过了什么请帮帮我:(

app.get('/lyrics', function(req, res)
{
axios.get('https://api.lyrics.ovh/v1/eminem/stan')
.then(function(data)
{
let result = {
lyrics : data['lyrics'],
};
res.status(200).send(result);
})
.catch(function(err)
{
if(err['statusCode'] == 404)
res.send({error : {status : 404, message : 'Lyrics not found'}});
else
res.send(err);
})
.then(function () {
// always executed
});
});

正如Phil所提到的,将您的代码编辑为这个,您就可以完成

app.get('/lyrics', function(req, res)
{
axios.get('https://api.lyrics.ovh/v1/eminem/stan')
.then(function(data)
{
let result = {
lyrics : data.data.lyrics,
};
res.status(200).send(result);
})
.catch(function(err)
{
if(err['statusCode'] == 404)
res.send({error : {status : 404, message : 'Lyrics not found'}});
else
res.send(err);
})
.then(function () {

});
});

最新更新