Reputation: 11830
I have this piece of code
import axios from 'axios';
import {
CURRENCY_RATE,
CURRENCY_FETCHING,
CURRENCY_ERROR
} from './type.js';
import {
CurrencyRateLink
} from '../urls.js';
import {currencyDetails} from './currencyDetails'
export const CurrencyRate = (selectedCurrency) => {
return function (dispatch) {
dispatch({type: CURRENCY_FETCHING})
axios.get(CurrencyRateLink).then((response) => {
let Currency = []
if ( selectedCurrency != "USD") {
let CurrencyRates = Object.keys(response.data.rates)
for (let i=0; i<CurrencyRates.length; i++) {
if (selectedCurrency == CurrencyRates[i]) {
console.log("inside If condition")
let currencySymbol = currencyDetails[selectedCurrency][symbol]
console.log(currencySymbol)
Currency.push({
currencyName : CurrencyRates[i],
currencyPrice : response.data.rates[selectedCurrency]
})
}
}
}
return (
dispatch({
//Dispatching Redux Action
})
)}).catch((error) => dispatch({
//Error Handling
}))
}
Here this statement isn't logging anything
console.log(currencySymbol)
but a statement just above it is logging in console log
console.log("inside If condition")
[Question:] What could be doing wrong?
[Note:] When I do something like let currencySymbol = currencyDetails[selectedCurrency]
there currencyDetails[selectedCurrency]
may not exist but then shouldn't it log undefined or throw an error instead of not logging anything?
[Update:] I am working on React-native
Upvotes: 1
Views: 4841
Reputation: 1201
You might getting Uncaught TypeError: Cannot read property XXX of undefined
but not visible on your react native cli.
if currencyDetails[selectedCurrency] == undefined
, then there will be above error situation
try below code:
let currencySymbol = (currencyDetails[selectedCurrency]) ? currencyDetails[selectedCurrency][symbol] : null
console.log('result', currencySymbol)
At lease you should get result null
Upvotes: 2
Reputation: 8418
SIDE NOTE - not directly related to question (title) but code structure
This statement
let CurrencyRates = Object.keys(response.data.rates)
gives you only array of keys, you can't use it (i
index) to access values (response.data.rates) ;)
Why not to use simple map
or for in
? According to this answer you can do sth like:
const rates = response.data.rates
Object.keys(rates).map(function(key, index) {
if( selectedCurrency == key) {
Currency.push({
currencyName : key,
currencyPrice : rates[key]
})
}
});
but even this isn't neccessary as you can just use property name to find value:
const rates = response.data.rates
if( rates[selectedCurrency] !== undefined ) {
Currency.push({
currencyName : selectedCurrency,
currencyPrice : rates[selectedCurrency]
})
}
Problem with currencySymbol = currencyDetails[selectedCurrency][symbol]
can't be solved without knowledge about currencyDetails
structure and expectations.
Upvotes: 0
Reputation: 3103
I have experienced this when working with React-Native and Expo... I found that sometimes my console.log()
would not appear in the terminal, but then I found that they could always be viewed through the Chrome developer tools during remote debugging.
If you are remote debugging check the console output of the browser. If you are not remote debugging, consider it.
Upvotes: 0
Reputation: 6171
currencyDetails[selectedCurrency][symbol]
may throws an error as currencyDetails[selectedCurrency]
is undefined. try to log that first.
If you are using promise: when an error happened it will call your catch block and not reporting any undefined error in the console.
const promise = new Promise((accept, reject) => {
accept(1);
})
promise.then(() => {
console.log('Inside then');
const data = {};
console.log('Should throw an error ' + data['abc']['def']);
console.log('After throw');
}).catch(error => {
console.log('Error will be happening here');
});
Upvotes: 1