控制台日志未记录

时间:2018-09-27 08:19:09

标签: javascript reactjs react-native

我有这段代码

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
   }))
  }

此语句未记录任何内容

console.log(currencySymbol)

但是上面的一条语句正在登录控制台日志

console.log("inside If condition")

[问题:] 可能出什么问题了?

[注意:] 当我执行类似let currencySymbol = currencyDetails[selectedCurrency]的操作时,currencyDetails[selectedCurrency]可能不存在,但它不应该记录未定义或抛出错误而不是不记录任何内容?

[更新:] 我正在研究React-native

4 个答案:

答案 0 :(得分:2)

您可能会得到Uncaught TypeError: Cannot read property XXX of undefined,但在本机cli上看不到。 如果currencyDetails[selectedCurrency] == undefined,则将出现上述错误情况

尝试以下代码:

let currencySymbol = (currencyDetails[selectedCurrency]) ? currencyDetails[selectedCurrency][symbol] : null
console.log('result', currencySymbol)

至少应获得result null

答案 1 :(得分:1)

currencyDetails[selectedCurrency][symbol]可能会引发错误,因为currencyDetails[selectedCurrency]未定义。尝试先登录。

如果您使用的是Promise:发生错误时,它将调用catch块,并且不会在控制台中报告任何未定义的错误。

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');
});

答案 2 :(得分:0)

我在使用React-Native和Expo时遇到了这种情况...我发现有时候我的console.log()不会出现在终端中,但是后来我发现可以随时通过Chrome开发者工具查看它们在远程调试期间。

如果要进行远程调试,请检查浏览器的控制台输出。如果您不是远程调试,请考虑一下。

答案 3 :(得分:0)

侧面说明-与问题(标题)不直接相关,但与代码结构

此声明

  let CurrencyRates = Object.keys(response.data.rates)

仅给您键数组,您不能使用它(i索引)来访问值(response.data.rates);)

为什么不使用简单的mapfor in?根据{{​​3}},您可以执行以下操作:

const rates = response.data.rates
Object.keys(rates).map(function(key, index) {
  if( selectedCurrency == key) {
    Currency.push({
          currencyName : key,
          currencyPrice : rates[key]
        })
  }
});

但这不是必需的,因为您可以使用属性名称来查找值

const rates = response.data.rates
if( rates[selectedCurrency] !== undefined ) {
    Currency.push({
          currencyName : selectedCurrency,
          currencyPrice : rates[selectedCurrency]
        })
}

currencySymbol = currencyDetails[selectedCurrency][symbol]的问题如果没有currencyDetails的结构和期望的知识就无法解决。