Reputation: 27
I'm working on a app with electron using axios to get api data, but when i use to display data it shows undefined in screen and when i output it, it shows the correct value!! Some help would be appreciated!
const electron = require('electron');
const path = require('path');
const BrowserWindow = electron.remote.BrowserWindow;
const axios = require('axios');
const notifyBtn = document.querySelector('.notify-btn');
const price = document.querySelector('.price');
const targetPrice = document.querySelector('.target-price');
function getBTC(){
axios.get('https://min-api.cryptocompare.com/data/price?fsym=BTC&tsyms=USD&api_key={api_key}')
.then(function(response) {
let cryptos = response.data;
price.innerHTML = '$'+cryptos;
console.log(response.data);
});
}
getBTC();
setInterval(getBTC, 30000);
I get a output in console: Object: USD: 3560.263(Current price of bitcoin) I get output on main screen: 'undefined'
I think its because it an object so how can i display an object? I may be wrong!! ThankYou!!
Upvotes: 1
Views: 412
Reputation: 913
You should use only primitive type variables when composing a string.
If you want to show an object, you could simply use JSON.stringify(cryptos)
to obtain the JSON string of the whole object.
Otherwise, you could print any other object property that is a primitive type, like cryptos.USD
.
Upvotes: 1
Reputation: 12874
It's not
price.innerHTML = '$'.cryptos;
// but
price.innerHTML = '$' + cryptos.USD;
Add .USD
because cryptos is an object. And the value is saved into the key USD
Upvotes: 1
Reputation: 101
What are you trying to achieve with '$'.cryptos; ?
If you are trying to concatenate some strings this is not how it works!
try "$"+cryptos
Upvotes: 1
Reputation: 54821
You are accessing the property of a string.
price.innerHTML = '$'.cryptos;
^^^ property
I think you wanted to concat values with a + operator
price.innerHTML = '$' + cryptos;
Upvotes: 1