[英]Console log not logging
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? [注意:]当我执行类似
let currencySymbol = currencyDetails[selectedCurrency]
, currencyDetails[selectedCurrency]
可能不存在但是它不应该记录未定义或抛出错误而不记录任何内容?
[Update:] I am working on React-native [更新:]我正在研究React-native
You might getting Uncaught TypeError: Cannot read property XXX of undefined
but not visible on your react native cli. 您可能会收到
Uncaught TypeError: Cannot read property XXX of undefined
但在您的反应本机cli上不可见。 if currencyDetails[selectedCurrency] == undefined
, then there will be above error situation 如果
currencyDetails[selectedCurrency] == undefined
,则会出现上述错误情况
try below code: 尝试以下代码:
let currencySymbol = (currencyDetails[selectedCurrency]) ? currencyDetails[selectedCurrency][symbol] : null
console.log('result', currencySymbol)
At lease you should get result null
至少你应该得到
result null
currencyDetails[selectedCurrency][symbol]
may throws an error as currencyDetails[selectedCurrency]
is undefined. currencyDetails[selectedCurrency][symbol]
可能会抛出错误,因为currencyDetails[selectedCurrency]
未定义。 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. 如果您正在使用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'); });
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. 我在使用React-Native和Expo时遇到过这种情况......我发现有时我的
console.log()
不会出现在终端中,但后来我发现在远程调试期间可以通过Chrome开发人员工具查看它们。
If you are remote debugging check the console output of the browser. 如果您是远程调试,请检查浏览器的控制台输出。 If you are not remote debugging, consider it.
如果您不是远程调试,请考虑它。
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) ;) 只给你一组键 ,你不能使用它(
i
索引)来访问值(response.data.rates);)
Why not to use simple map
or for in
? 为什么不使用简单的
map
或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. 如果没有关于
currencyDetails
结构和期望的知识,则无法解决currencySymbol = currencyDetails[selectedCurrency][symbol]
问题 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.