2

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

iRohitBhatia
  • 3,808
  • 6
  • 38
  • 88
  • is there just an empty line in the log? Maybe the field is there but empty (i.e. not null but just containing the empty string `""`). Try using the debugger to look at the variable values, or do something like `console.log(JSON.stringify(currencyDetails[selectedCurrency]));` and see what properties are inside the selectedCurrency, and what values they have – ADyson Sep 27 '18 at 08:21
  • 6
    Rather than stumbling around in the dark with a `console.log` torch, *turn on the lights* by using the powerful debugger built into your browser to step through the code statement by statement, inspecting your variables while execution is stopped, etc. Search for " developers tools" to learn how to use the debugger. Perhaps the code *is* throwing an error but something is catching and swallowing it. – T.J. Crowder Sep 27 '18 at 08:22
  • do you have any sort of console filters enabled ? – apokryfos Sep 27 '18 at 08:25
  • @apokryfos Nope – iRohitBhatia Sep 27 '18 at 08:25
  • @ADyson that doesn't work either. – iRohitBhatia Sep 27 '18 at 08:27
  • @T.J.Crowder Trying that :) – iRohitBhatia Sep 27 '18 at 08:28
  • "doesn't work" means what exactly? What output do you get? Try `console.log(JSON.stringify(currencyDetails));` instead. Try `alert(JSON.stringify(currencyDetails));` instead even. Or better still as T.J. Crowder said...**use the debugger** – ADyson Sep 27 '18 at 08:28
  • @ADyson I mean, my console isn't logging that as well. I can't try `alert(JSON.stringify(currencyDetails));` since it is `react-native` but still thanks, I am trying what T.J Crowder said :) – iRohitBhatia Sep 27 '18 at 08:31

4 Answers4

2

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

Nuwan Attanayake
  • 921
  • 8
  • 19
1

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');
});
Daniel Tran
  • 5,531
  • 9
  • 22
  • Updated my code Also, It isn't even throwing any errors :) – iRohitBhatia Sep 27 '18 at 08:23
  • 2
    As the OP mentions in the edit at the end of the question, if that were happening, he/she would be seeing an error in the console (unless of course something is catching and swallowing errors). *(not my downvote, and it seems harsh)* – T.J. Crowder Sep 27 '18 at 08:23
  • 2
    Update the answer with explanation. You need to verify if your catch block is called or not. – Daniel Tran Sep 27 '18 at 08:34
  • @DanielTran I simply did a console.log in `catch` statement to realize that I should've done something like this ["symbol"]. Though I still expected it to throw an error as you previously pointed it out. – iRohitBhatia Sep 27 '18 at 10:23
  • 2
    Yeah it did throw an error to your catch block. You just ignored it – Daniel Tran Sep 27 '18 at 10:40
0

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.

Joey Gough
  • 2,096
  • 2
  • 13
  • 33
0

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.

xadm
  • 6,900
  • 3
  • 8
  • 17