我正在使用Spotify API来获取给定类型的推荐。我已经编写了获取建议的函数(这是一个异步事件,因为它是从Spotify API中获取的)并返回它。当我通过将其合并到日志中来验证它时,我收到的值非常好,但是当我返回该值时,调用函数将收到“未定义”。在给定的代码中,在底部,我将返回“ body”,该主体将作为未定义返回。有人可以给我提示如何解决此问题吗?
// List of viable genres to search, provided by Spotify API
const genreObj = {
genres: [
"acoustic",
"afrobeat",
"alt-rock",
"alternative",
"ambient",
"anime"
]
};
// Build string from inputs
let genreString = "";
for (let i = 0; i < genres.length - 1; i++) {
if (genreObj.genres.includes(genres[i].toLowerCase())) {
genreString += genres[i].toLowerCase() + "%2C";
}
}
genreString += genres[genres.length - 1].toLowerCase();
request.post(authOptions, async function(error, response, body) {
if (!error && response.statusCode === 200) {
// use the access token to access the Spotify Web API
var token = body.access_token;
var options = {
url:
"https://api.spotify.com/v1/recommendations?limit=10&market=US&seed_genres=" +
genreString,
headers: {
Authorization: "Bearer " + token
},
json: true
};
request.get(options, async function(error, response, body) {
console.log(body);
return body;
});
}
});
};