Empty array getting logged in, in nodeJs

115
June 01, 2021, at 07:40 AM

Flow:

  • The user submits a queryValue in index.html.
  • Three API calls are made (using a function called ytAxiosGetFunc) based on the queryValue.
  • The returned values are put in three arrays: ytQueryAppJs, ytCoverAppJs and ytLiveAppJs.
  • ytCoverAppJs and ytLiveAppJs contains redundant values. These are removed using a function called compareAndRemove.
  • Two new arrays are allocated which contain unique values from for each of these respectively. These are ytCoverUniqueAppJs and ytLiveUniqueAppJs.
  • Hence, a total of five arrays get logged in console, based on the query.

Expected Console Log:

All the arrays are filled.

Current Console Log:

All the arrays are filled, except ytCoverUniqueAppJs and ytLiveUniqueAppJs. These are empty.

Source Code from 'app.js':

// https://stackoverflow.com/a/14930567/14597561
function compareAndRemove(removeFromThis, compareToThis) {
  return (removeFromThis = removeFromThis.filter(val => !compareToThis.includes(val)));
}
// Declaring variables for the function 'ytAxiosGetFunc'
let apiKey = "";
let urlOfYtAxiosGetFunc = "";
let ytResponse = "";
let ytExtractedResult = [];
// This function GETs data, parses it, allocates required values in an array.
async function ytAxiosGetFunc(queryOfYtAxiosGetFunc, maxResultsOfYtAxiosGetFunc) {
  apiKey = "AI...5U"
  urlOfYtAxiosGetFunc = "https://www.googleapis.com/youtube/v3/search?key=" + apiKey + "&part=snippet&order=relevance&type=video";
  try {
    ytResponse = await axios({
      url: urlOfYtAxiosGetFunc,
      method: "get",
      params: {
        q: queryOfYtAxiosGetFunc,
        maxResults: maxResultsOfYtAxiosGetFunc
      }
    })
    let ytResult = ytResponse.data;
    for (i = 0; i < (ytResult.items).length; i++) {
      ytExtractedResult[i] = ytResult.items[i].id.videoId;
      // console.log(ytExtractedResult);
    }
    return (ytExtractedResult);
    ytExtractedResult.length = 0;
    ytResponse.length = 0;
  } catch (e) {
    console.log(e);
  }
}
app.post("/", async function(req, res) {
  // Accessing the queryValue user submitted in index.html.
  query = req.body.queryValue;
  // Fetcing top results related to user's query and putting them in the array.
  ytQueryAppJs = await ytAxiosGetFunc(query, 4);
  console.log("ytQueryAppJs");
  console.log(ytQueryAppJs);

  // Fetching 'cover' songs related to user's query and putting them in the array.
  if (query.includes("cover") == true) {
    ytCoverAppJs = await ytAxiosGetFunc(query, 8);
    console.log("ytCoverAppJs");
    console.log(ytCoverAppJs);
    // Removing redundant values.
    ytCoverUniqueAppJs = compareAndRemove(ytCoverAppJs, ytQueryAppJs);
    console.log("ytCoverUniqueAppJs:");
    console.log(ytCoverUniqueAppJs);
  } else if (query.includes("live") == true) {
    ytCoverAppJs = await ytAxiosGetFunc(query.replace("live", " cover "), 8);
    console.log("ytCoverAppJs");
    console.log(ytCoverAppJs);
    // Removing redundant values.
    ytCoverUniqueAppJs = compareAndRemove(ytCoverAppJs, ytQueryAppJs);
    console.log("ytCoverUniqueAppJs:");
    console.log(ytCoverUniqueAppJs);
  } else {
    ytCoverAppJs = await ytAxiosGetFunc(query + " cover ", 8);
    console.log("ytCoverAppJs");
    console.log(ytCoverAppJs);
    // Removing redundant values.
    ytCoverUniqueAppJs = compareAndRemove(ytCoverAppJs, ytQueryAppJs);
    console.log("ytCoverUniqueAppJs:");
    console.log(ytCoverUniqueAppJs);
  }
  // Fetching 'live performances' related to user's query and putting them in the array.
  if (query.includes("live") == true) {
    ytLiveAppJs = await ytAxiosGetFunc(query, 8);
    console.log("ytLiveAppJs");
    console.log(ytLiveAppJs);
    // Removing redundant values.
    ytLiveUniqueAppJs = compareAndRemove(ytLiveAppJs, ytQueryAppJs.concat(ytCoverUniqueAppJs));
    console.log("ytLiveUniqueAppJs:");
    console.log(ytLiveUniqueAppJs);
  } else if (query.includes("cover") == true) {
    ytLiveAppJs = await ytAxiosGetFunc(query.replace("cover", " live "), 8);
    console.log("ytLiveAppJs");
    console.log(ytLiveAppJs);
    // Removing redundant values.
    ytLiveUniqueAppJs = compareAndRemove(ytLiveAppJs, ytQueryAppJs.concat(ytCoverUniqueAppJs));
    console.log("ytLiveUniqueAppJs:");
    console.log(ytLiveUniqueAppJs);
  } else {
    ytLiveAppJs = await ytAxiosGetFunc(query + " live ", 8);
    console.log("ytLiveAppJs");
    console.log(ytLiveAppJs);
    // Removing redundant values.
    ytLiveUniqueAppJs = compareAndRemove(ytLiveAppJs, ytQueryAppJs.concat(ytCoverUniqueAppJs));
    console.log("ytLiveUniqueAppJs:");
    console.log(ytLiveUniqueAppJs);
  }
  // Emptying all the arrays.
  ytQueryAppJs.length = 0;
  ytCoverAppJs.length = 0;
  ytCoverUniqueAppJs.length = 0;
  ytLiveAppJs.length = 0;
  ytLiveUniqueAppJs.length = 0;
});

(I am a beginner. Please guide and suggest a title to categorize this question for coming viewers.)

Rent Charter Buses Company
READ ALSO
500 Error code with &ldquo;Internal server Error&rdquo; message, while doing POST method using MVVM and API in android application

500 Error code with “Internal server Error” message, while doing POST method using MVVM and API in android application

For API confirmation, I have tried in POSTMAN app, But response is getting properly and the data has been inserted successfullyBelow is the result of POSTMAN

144
How do I get the value of useState from component A to component B

How do I get the value of useState from component A to component B

I'm relatively new to JS and React as a whole, I delved into React and started making some projects with Django backendI have a list of items in the ProductList component in react,

87
Node application crashes due to mysql error &#39;PROTOCOL_PACKETS_OUT_OF_ORDER&#39;

Node application crashes due to mysql error 'PROTOCOL_PACKETS_OUT_OF_ORDER'

I am running a nodejs application (web service using express) on RHEL v8, using node v16

159
Javascript and jquery I can&#39;t read value from array index

Javascript and jquery I can't read value from array index

I have a list of numbers (ex"20,145") that returns from an ajax call and I am pushing them to a js array

98