0

I am trying to return the .data property value of the object that is grabbed after hitting this URL https://jsonmock.hackerrank.com/api/stocks?date=23-February-2000. but for some reason, it is not returning any result. even though I have a return resultO

Here is the code, I tried to research it but I am a bit stuck thank you!!

async function getStockInformation(date) {
    // write your code here
    // API endpoint: https://jsonmock.hackerrank.com/api/stocks?date=<date>
    
//     let str = date;
//   let leadingNum = "";
//   let i = 0;

//   while (str[i] != "-") {
//     leadingNum += str[i];
//     i++;
//   }
//   let newDate = parseInt(leadingNum);
//   newDate = newDate.toString();
//   let currentDate = newDate;
//   let newstr = date.slice(i, date.length);
//   currentDate = currentDate + newstr;
  let resultO=""

  https.get(
    "https://jsonmock.hackerrank.com/api/stocks?date=5-January-2000",
    (response) => {
      let obj = "";
      response.on("data", (res) => {
        obj += res;
        console.log(obj)
      });

      response.on("end", () => {
        let newt = JSON.parse(obj).data;
        resultO=newt[0].open;
        // "Open:" + newt[0].open;
        // resultO=newt[0].data
        // console.log(resultO)
        // "High:" + newt[0].high;
        // "Low:" + newt[0].low;
        // "Close:" + newt[0].close;
      
      });
    }
  );
    return resultO
  
}
lipo
  • 117
  • 1
  • 7
  • 1
    why is `getStockInformation` async? you don't await (or use promises at all). ignore the other thing I said - your indentation was confusing – Bravo Jul 22 '21 at 03:16
  • You might also be interested in https://www.npmjs.com/package/promisify though if you're importing libraries, you might as well just use `node-fetch` or Axios – Phil Jul 22 '21 at 03:18
  • 1
    firstly, remove `async` since it's irrelevant - then ,decide if you want to return a Promise, or use a callback to send the response to the caller – Bravo Jul 22 '21 at 03:19
  • unfortunately I cannot use npm packages and have to use https only – lipo Jul 22 '21 at 03:19
  • Then you'll want to return a [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) that resolves within the `https()` callback or pass a callback function in to your `getStockInformation` function – Phil Jul 22 '21 at 03:20

0 Answers0