I found these answers, but couldn't figure how to implement:
There is a 'callback' function in app.post
at the bottom of the code.
It is supposed to return the array created in httpsYtGetFunc
.
It returns the array with null
value. Rest of the array is empty.
app.js
// Declaring variables for the function 'httpsYtGetFunc'
let apiKey = "";
let urlOfYtGetFunc = "";
let resultOfYtGetFunc = "";
let extractedResultOfYtGetFunc = [];
// This function GETs data, parses it, pushes required values in an array.
function httpsYtGetFunc(queryOfYtGetFunc, callback) {
apiKey = "AI...MI"
urlOfYtGetFunc = "https://www.googleapis.com/youtube/v3/search?key=" + apiKey + "&part=snippet&q=" + queryOfYtGetFunc + "&maxResults=4&order=relevance&type=video";
// GETting data and storing it in chunks.
https.get(urlOfYtGetFunc, (response) => {
const chunks = []
response.on('data', (d) => {
chunks.push(d)
})
// Parsing the chunks
response.on('end', () => {
resultOfYtGetFunc = JSON.parse((Buffer.concat(chunks).toString()))
// console.log(resultOfYtGetFunc)
// Extracting useful data, and allocating it.
for (i = 0; i < (resultOfYtGetFunc.items).length; i++) {
extractedResultOfYtGetFunc.push(resultOfYtGetFunc.items[i].id.videoId);
// console.log(extractedResultOfYtGetFunc);
}
})
})
callback (null, extractedResultOfYtGetFunc);
}
// Client makes POST request.
app.post("/", function(req, res) {
query = "niall";
// The callback
ytQueryAppJs = httpsYtGetFunc(query, (ytQueryAppJs) => {
console.log("ytQueryAppJs:");
console.log(ytQueryAppJs);
});
});
The console logs only null
.
I believe that callback (null, extractedResultOfYtGetFunc);
is running before https.get
finishes.
Could somebody please suggest how to fix it and log all the results?
Thank you so much.
This is the link to original question: