1

I am very new to web programming, and I need a help.

I am using Node.js to fetch data using RapidAPI The fetched result returns me with Parsed JSON format in array. However, if I were to give an index, it's returning alphabet instead of item I wanted to see.

Below is the code I have to fetch Apple result:

const express = require('express');
const bodyParser = require('body-parser');
const http = require("https");

const app = express();
app.use(bodyParser.urlencoded({extended:true}));

app.get("/", function(request, response){
    response.sendFile(__dirname + "/index.html");
});

app.post("/", function(request, response){
const options = {
    "method": "get",
    "hostname": "rapidapi.p.rapidapi.com",
    "port": null,
    "path": "/income-statement/AAPL?apikey=demo",
    "headers": {
        "x-rapidapi-key": "895157e459mshecb81dbe427f124p1fe70cjsn772a488898eb",
        "x-rapidapi-host": "financial-modeling-prep.p.rapidapi.com",
        "useQueryString": true
    }
};

const req = http.request(options, function (res) {
    const chunks = [];
    
    if (res.statusCode === 200) {
        console.log("Success");
    } else {
        console.log("Fail");
    }

    res.on("data", function (chunk) {
        console.log(chunk.toString('utf-8')[23]);
        chunks.push(chunk);
    });

    res.on("end", function () {
        const body = Buffer.concat(chunks); 

    });
});    
req.end();
});

LOG RESULT of "chunk":
[38 items
0:{46 items
"date":"2020-09-26"
"symbol":"AAPL"
"fillingDate":"2020-10-30"
"acceptedDate":"2020-10-29 18:06:25"
"period":"FY"
"cashAndCashEquivalents":38016000000
"shortTermInvestments":52927000000
"cashAndShortTermInvestments":90943000000
"netReceivables":16120000000
"inventory":4061000000
"otherCurrentAssets":32589000000
"totalCurrentAssets":143713000000
"propertyPlantEquipmentNet":36766000000
"goodwill":0
"intangibleAssets":0
"goodwillAndIntangibleAssets":0
"longTermInvestments":100887000000
"taxAssets":0
"otherNonCurrentAssets":42522000000
"totalNonCurrentAssets":180175000000
"otherAssets":90482000000
"totalAssets":323888000000
"accountPayables":42296000000
"shortTermDebt":8773000000
"taxPayables":0
"deferredRevenue":6643000000
"otherCurrentLiabilities":47680000000
"totalCurrentLiabilities":105392000000
"longTermDebt":98667000000
"deferredRevenueNonCurrent":0
"deferredTaxLiabilitiesNonCurrent":0
"otherNonCurrentLiabilities":54490000000
"totalNonCurrentLiabilities":153157000000
"otherLiabilities":0
"totalLiabilities":258549000000
"commonStock":16976763000
"retainedEarnings":14966000000
"accumulatedOtherComprehensiveIncomeLoss":-406000000
"othertotalStockholdersEquity":33802237000
"totalStockholdersEquity":65339000000
"totalLiabilitiesAndStockholdersEquity":323888000000
"totalInvestments":153814000000
"totalDebt":107440000000
"netDebt":69424000000
"link":"https://www.sec.gov/Archives/edgar/data/320193/000032019320000096/0000320193-20-000096-index.htm"
"finalLink":"https://www.sec.gov/Archives/edgar/data/320193/000032019320000096/aapl-20200926.htm"
},...]

Question: If I specifically wanted to access specific field like "netDebt" from the response, how would I access it?

ex) chunck[0] returns a letter like "l". I guess I am not clearly understanding how it's structured. I was thinking something like chunk[0]["netDebt"]

Thank you,

Grokify
  • 15,092
  • 6
  • 60
  • 81

1 Answers1

0

The response is the array of objects. If you want to access a particular key-value pair from an array of objects, you can do this in the following manner.

const arr = [
    {
        "name": "Pratham",
        "age": 22
    }, 
    {
        "twitter": "prathkum",
        "followers": 116000
    }
];

console.log(arr[0].name); // Pratham
console.log(arr[1].followers); // 116000

So if you want to access the netDebt key from the chunk array, you can do this like this.

chunk[0].netDebt // 69424000000

P.S. You just exposed your API key in the code snippet that you posted in your question. An API key is sensitive data and now it's accessible publicly. I recommend you delete this key and generate a new one for you. You can delete and generate new from the Developer Dashboard.

Pratham
  • 497
  • 3
  • 7