I am currently working on a webproject for a Twitch-Community in node.js. I'm using the Twitch API to receive the profilepictures and adding them with other information into an array of objects, before the page will be rendered. Unfortunately I'll only get a pending promise for the profile pictures URL, when the page is rendered. nearly a millisecond after the page will be rendered, I get the console log about the PB-Links, so it goes fast, so an ajax dosen't make sense in my opinion. Is there any possibility to wait until the promises are loaded before the script goes on to render the page?
Thank you in advance
async function getStreamersPB(streamerName){
var streamer = await twitch.getUsers(streamerName);
var streamerPBURL = await streamer.data[0].profile_image_url;
console.log(streamerPBURL);
}
...
router.get('/', (req, res) => {
var streamer = [];
con.query("SELECT * FROM xxx", function (err, result) {
if (err) {
console.log(err);
}
if (result.length !== 0){
for (let i = 0; i < result.length; i++) {
let streamObj = {
"id": result[i].id,
"name": result[i].name,
"website": result[i].website.toLowerCase(),
"status": result[i].status,
"rang": result[i].rang,
"userPB": getStreamersPB(result[i].name)
}
console.log(streamObj);
streamer.push(streamObj);
}}});
con.query("SELECT * FROM xxx WHERE xxx = '" + xxx + "'", function (err, result) {
if (err) {
console.log(err);
} if(result.length === 1){
res.render('home', {
message: result[0].twitchName,
userID: result[0].id,
streamer: streamer
});
...
That is my Console log:
{
id: 1,
name: 'xxx',
website: 'xxx',
status: 0,
rang: 1,
userPB: Promise { <pending> }
}
https://static-cdn.jtvnw.net/...xxx