-1

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:

How to synchronize 'time-consuming value allotment' of a variable, with the next commands, in nodejs?

2
  • Does this answer your question? How to return the response from an asynchronous call? Commented May 30, 2021 at 7:41
  • @jonrsharpe Thank you for looking it up. I have tried the method "2. Restructure Code" of the answer in the question above. I'm sharing the problem that I faced while implementing it. Commented May 30, 2021 at 7:49

2 Answers 2

1

response.on('end', () associate a function to the "end" event of https.get method. When you call callback (null, extractedResultOfYtGetFunc); outside of the https.get() block, the https request has not been finish yet, and the extractedResultOfYtGetFunc array is empty.

You need to move the callback function to the response.on('end') block

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); // move the callback here
    })  
  })
  // callback (null, extractedResultOfYtGetFunc);
}

EDIT

You will need to add 1 more parameter in the callback function to receive the error, if any. ytQueryAppJs = httpsYtGetFunc(query, (err, ytQueryAppJs) => {

Sign up to request clarification or add additional context in comments.

11 Comments

This seems to solve the issue. I do not know why but I'm returned with TypeError: Cannot set property 'length' of undefined. I checked, and the API quota limit has not exceeded. (I can't verify if this has, indeed, solved the issue.)
Thank you so much, pal. I had some unnecessary code 'uncommented', which was causing the issue. On a side note, could you please elaborate how do we use response.on('error')?
I read the document here, the response.on('error') is used to handle error when you send the request and cannot receive the desired response. For example, when you received error 404. It makes the code more reliable
Update: I had to remove the null argument to make it work.
Oh, alright. .on('error', (e) => { console.error(e); }); This is what I was looking for. :)
|
-1

I'm not Node.js developer but I belive that callback (null, extractedResultOfYtGetFunc); need to be called in http.get callback function.

EDITED move to response.on('end' callback

Something like that:

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);
    })
  })
}

2 Comments

Yes, I tried doing the same. But it didn't work.
Then try inside response.on('end'... it seems to be called at the end of respons accoriding to (stackoverflow.com/questions/36659002/…) "...The two you have above are response.on('data'...) which is when you get the data and response.on('end'...) is at the end of the response, simple as that!"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.