gpt4 book ai didi

javascript - 控制台日志不记录

转载 作者:行者123 更新时间:2023-11-30 11:13:43 25 4
gpt4 key购买 nike

我有这段代码

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] 可能不存在,但随后它不应该记录 undefined 或抛出错误而不是不记录任何东西吗?

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

最佳答案

您可能会收到 Uncaught TypeError: Cannot read property XXX of undefined 但在您的 React Native CLI 上不可见。如果currencyDetails[selectedCurrency] == undefined,就会出现上面的错误情况

试试下面的代码:

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

至少你应该得到 result null

关于javascript - 控制台日志不记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52532486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com