I am a new user of React.js. I have an array of objects that I am simply trying to log to the console. I can log the full "metrics" Array, but when I try to access a specific index; I get a message of "Cannot read properties of undefined (reading '1') "
I need to access this array, so that I may make use of the values on the UI. The console screen shot is attached...console.log() screenshot
Here is a sample of the data being read:
{isAvailable: true, state: 'API State: New API data received', timestamp: '2021-11-08 14:56:51.867426', metrics: Array(147), Symbol(topic): 'my-topic'} isAvailable: true metrics: Array(147) [0 … 99] 0: {name: 'ESM_DATA.ESM_INFOS[1].TICK_COUNT', value: '1472700451', type: 'DINT'} 1: {name: 'UT59_DO_RDOL', value: false, type: 'BOOL'} 2: {name: 'UT51_DO_BF', value: false, type: 'BOOL'} 3: {name: 'UT129_DNL', value: false, type: 'BOOL'}
The code I am using looks like this...
const PubSubFunctional = () => {
const [arrayData, setArrayData] = useState([]);
useEffect(() => {
PubSub.subscribe('testingtopic').subscribe({
next: (data) => {
try {
setArrayData(data.metrics);
//console.log('value', data.value);
} catch (error) {
console.log('Error, Try again!');
}
},
error: (error) => console.error(error),
close: () => console.log('Done'),
});
}, []);
console.log('1:', arrayData[1]);