返回对象 4 ..尝试显示对象数据。
/* API ROUTES */
router.get('/', (req, res) => {
// Get artist
const data = getArtist('Drake');
if(data) {
console.log(data.id);
} else {
console.log('nothing in data');
}
});
// Get an artist name
function getArtist(name) {
spotify
.search({ type: 'artist', query: name })
.then(function(response) {
// Gets Name and Id of the artist
const data = {
"id": response.artists.items[1].id,
"name": response.artists.items[1].name
}
return data;
})
.catch(function(err) {
console.log(err);
});
}
显示错误:数据中无内容。
来自JAVA不确定我在Javascript中做错了什么。有想法吗?有更好的方法吗?
答案 0 :(得分:1)
Try This:
router.get('/', (req, res) => {
// Get artist
getArtist('Drake').then( (response) => {
if(response) {
const data = {
"id": response.artists.items[1].id,
"name": response.artists.items[1].name
}
console.log(data.id);
} else {
console.log('nothing in data');
}
});
});
// Get an artist name
function getArtist(name) {
return spotify.search({ type: 'artist', query: name });
}