【发布时间】:2020-05-17 10:03:03
【问题描述】:
我尝试获取带有统计信息的 Youtube 特定频道 ID 的最后上传视频列表。我得到了列表,但没有得到统计数据。
我认为结果是在对象分配之前返回的。对象赋值后如何返回结果?
这是我的代码:
async function (inputs, exits) {
var { google } = require('googleapis');
var youtubeApiKey = 'API_KEY';
var service = google.youtube('v3');
// Get upload ID to list uploaded videos
service.channels.list({
part: 'contentDetails',
id: inputs.ytbIdChannel,
key: youtubeApiKey,
})
.then(res => {
if (res.data.items[0].contentDetails.relatedPlaylists.uploads) {
let uploadsId = res.data.items[0].contentDetails.relatedPlaylists.uploads;
// With the uploadID, get all uploaded videos
service.playlistItems.list({
part: 'snippet',
playlistId: uploadsId,
maxResults: '12',
key: youtubeApiKey,
})
.then(res => {
this.youtbeVideos = res.data.items,
// For each videos in list, inject the statistics object and return all videos
this.youtbeVideos.forEach(video => {
let videoId = video.snippet.resourceId.videoId
service.videos.list({
part: 'statistics',
id: videoId,
key: youtubeApiKey,
})
.then(res => {
Object.assign(video, res.data.items[0].statistics)
return exits.success(
this.youtbeVideos
);
})
.catch(error => {
console.error(error);
});
});
})
.catch(error => {
console.error(error);
});
}
})
.catch(error => {
console.error(error);
});
}
感谢您的帮助
【问题讨论】:
标签: node.js promise youtube-api