此功能使用Spotify API创建播放列表。它返回一个包含' id'我需要的。
function createPlaylist(pc) {
let index = partyCodeExists(pc)
let at = token_arr[index]
let userID = user_ids[index]
var authOptions = {
url: 'https://api.spotify.com/v1/users/' + userID + '/playlists',
body: JSON.stringify({
'name': pc,
'description': 'Jukibox playlist',
'public': false
}),
dataType: 'json',
headers: {
'Authorization': 'Bearer ' + at,
'Content-Type': 'application/json'
}
};
request.post(authOptions, function (error, response, body) {
console.log(body);
console.log(body.id);
});
};
下层控制台日志提供以下输出:
{
"collaborative" : false,
"description" : null,
"external_urls" : {
"spotify" : "https://open.spotify.com/user/larsonadam-us/playlist/5pZ3C6ZRB3C9Tv9Z3EdA0g"
},
"followers" : {
"href" : null,
"total" : 0
},
"href" : "https://api.spotify.com/v1/users/larsonadam-us/playlists/5pZ3C6ZRB3C9Tv9Z3EdA0g",
"id" : "5pZ3C6ZRB3C9Tv9Z3EdA0g",
"images" : [ ],
"name" : null,
"owner" : {
"display_name" : null,
"external_urls" : {
"spotify" : "https://open.spotify.com/user/larsonadam-us"
},
"href" : "https://api.spotify.com/v1/users/larsonadam-us",
"id" : "larsonadam-us",
"type" : "user",
"uri" : "spotify:user:larsonadam-us"
},
"primary_color" : null,
"public" : true,
"snapshot_id" : "90UhGQhCi0Xq2vch8g/wrQ5BZoKK5cmIkiwybqJLUIsEAljI+L90YPzpD59T8zwP",
"tracks" : {
"href" : "https://api.spotify.com/v1/users/larsonadam-us/playlists/5pZ3C6ZRB3C9Tv9Z3EdA0g/tracks",
"items" : [ ],
"limit" : 100,
"next" : null,
"offset" : 0,
"previous" : null,
"total" : 0
},
"type" : "playlist",
"uri" : "spotify:user:larsonadam-us:playlist:5pZ3C6ZRB3C9Tv9Z3EdA0g"
}
undefined
为什么第二个选项显示未定义?
另一个注意事项,我有身体解析器,它可以在代码的其他区域工作
答案 0 :(得分:1)
问题是返回是一个字符串。您需要在获得ID之前处理退货。试试这个。
console.log(body)
let bodyObject = JSON.parse(body)
console.log(bodyObject.id)