简体   繁体   中英

API Being Super Annoying

This API (worldtradingdata) is getting mixed up, and its really annoying me. Please let me know what I'm doing wrong! It's spewing out the percents for the other ones, like the Apple percent is being displayed for GE and stuff.

'''

var temp=[];var i;var jew;var adbe;var orcl;var xlk;var pypl;var ibm;
const url = new URL(
    "https://api.worldtradingdata.com/api/v1/stock"
);
function price(symbol) {
let params = {
    "symbol": (symbol),
    "api_token": "CtE9BRvfbrygbjgMZorx5A8fhs0t1rBKOINinkWoG3aSNqLAp40jdELocr29",
};
Object.keys(params)
    .forEach(key => url.searchParams.append(key, params[key]));

fetch(url, {
    method: "GET",
})
    .then(response => response.json())
    .then(json => jew = ((Object.values(json))[2]));
}
function getPrice(symbols) {
price(symbols);
setTimeout(function(){
for (i = 0; i < 3; i++) {
//console.log(symbols.split(",")[i]);
temp[i] = (Object.values(jew[i]));
}
console.log(temp);
}, 1000);
}
function israel() {
getPrice("XLK,IBM,ORCL,AAPL,GE");
}
israel();

'''

This will work

 var temp=[];var i;var jew;var adbe;var orcl;var xlk;var pypl;var ibm; const url = new URL( "https://api.worldtradingdata.com/api/v1/stock" ); function price(symbol) { let params = { "symbol": (symbol), "api_token": "CtE9BRvfbrygbjgMZorx5A8fhs0t1rBKOINinkWoG3aSNqLAp40jdELocr29", }; Object.keys(params) .forEach(key => url.searchParams.append(key, params[key])); fetch(url, { method: "GET", }) .then(response => response.json()) .then(json => { jew = ((Object.values(json))[2]); setTimeout(function(){ for (i = 0; i < 3; i++) { //console.log(symbols.split(",")[i]); temp[i] = (Object.values(jew[i])); } console.log(temp); }, 1000); }); } function getPrice(symbols) { price(symbols); } function israel() { getPrice("XLK,IBM,ORCL,AAPL,GE"); } israel();

What is happening in your case

Your code is trying to access the array data of jew before we get the response from API.

What I did is after getting response from API, I am trying to access and console the data.

I got the following console from your code:

[
  [
    "AAPL",
    "Apple Inc.",
    "USD",
    "321.45",
    "323.52",
    "324.73",
    "318.96",
    "327.85",
    "168.42",
    "2.60",
    "0.82",
    "318.85",
    "1406497980416",
    "27626219",
    "42035614",
    "4375479808",
    "NASDAQ Stock Exchange",
    "NASDAQ",
    "EST",
    "America/New_York",
    "-18000",
    "2020-02-05 16:00:01",
    "25.52",
    "12.60"
  ],
  [
    "GE",
    "General Electric Company",
    "USD",
    "12.86",
    "12.76",
    "12.86",
    "12.57",
    "13.00",
    "7.65",
    "0.28",
    "2.23",
    "12.58",
    "112313442304",
    "49050075",
    "85669571",
    "8733549568",
    "New York Stock Exchange",
    "NYSE",
    "EST",
    "America/New_York",
    "-18000",
    "2020-02-05 16:00:04",
    "N/A",
    "-0.62"
  ],
  [
    "IBM",
    "International Business Machines Corporation",
    "USD",
    "156.33",
    "150.21",
    "157.44",
    "150.15",
    "157.44",
    "126.85",
    "7.22",
    "4.84",
    "149.11",
    "136047435776",
    "13913469",
    "7033114",
    "885636992",
    "New York Stock Exchange",
    "NYSE",
    "EST",
    "America/New_York",
    "-18000",
    "2020-02-05 16:00:29",
    "14.80",
    "10.57"
  ]
]

Seems like you are getting the right values. I have verified against the live data in the site https://www.worldtradingdata.com/stock/GE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM