I have a function called uploadVideoOnVimeo that uploads videos to vimeo using the VIMEO SDK.
In my controller I use uploadVideoOnVimeo to upload the video and when the function finishes running I would like to run the rest of my code.
async function uploadVideoOnVimeo(file_name: string) {
try {
let videoId = 0;
client.upload(
`uploads/${file_name}`,
{
name: "Test",
description: "The description goes here.",
},
function (uri: any, name: any) {
let regexOnlyNumber = new RegExp(/\d+/);
videoId = parseInt(uri.match(regexOnlyNumber));
console.log("Your video URI is: " + uri);
console.log("Your video Id is: " + videoId);
fs.unlink(`uploads/${file_name}`, async (err) => {
if (err) console.log(err);
else {
console.log(`\nDeleted file: ${file_name}`);
const response = await getUrlVideoOnVimeo(videoId);
// console.log(response.player_embed_url);
// console.log(response.embed.html);
// console.log(response.pictures.sizes.find((f: any) => f.width === 200));
const { link } = response.pictures.sizes.find(
(f: any) => f.width === 200
);
let payload = {
externalVideoId: videoId,
videoUrl: response.player_embed_url,
thumbnailUrl: link,
};
return payload;
}
});
},
function (bytes_uploaded: any, bytes_total: any) {
var percentage = ((bytes_uploaded / bytes_total) * 100).toFixed(2);
console.log(bytes_uploaded, bytes_total, percentage + "%");
},
function (error: any) {
console.log("Failed because: " + error);
}
);
} catch (error) {
console.log(error);
return null;
}
}
async function getUrlVideoOnVimeo(videoId: number) {
const headers = {
Authorization: `bearer ${access_token}`,
};
const response = await axios.get(`https://api.vimeo.com/videos/${videoId}`, {
headers,
});
return response.data;
}
That is, I would just like to run the second part of my code when the uploadVideoOnVimeo is done.
Below My controller:
const videoUploaded = await uploadVideoOnVimeo(fileName);
console.log(`test ${videoUploaded}`)
return res.send(videoUploaded)
So what's happening is this part of my code is running before the uploadVideoOnVimeo finishes:
console.log(`test ${videoUploaded}`)
return res.send(videoUploaded)
So now, my output is being this:
test undefined
Your video URI is: /videos/753188733
Your video Id is: 753188733
Deleted file: 1ada4ce073453e654c6dce43aa20bb40
My variable videoUploaded is undefined.