I try to get the last video list uploaded of a specific channel ID of Youtube with statistics. I get the list but not statistics.
I think the result is returning before Object assign. How can I return the result after Object assign ?
This my code :
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);
});
}
Thanks for your help