首页 > 解决方案 > ReactJS - console.log(ObjectName) //Working console.log(ObjectName.property) //Property not recognized

问题描述

I will need help with the following code please

import React, {useEffect, useState} from "react";
import APIService from './service/ApiService'

export default function App() {

    useEffect(() => {
        APIService.getAdminApp('KYC').then(donnees => { //stats
            updateStats(donnees)
        }).catch(function (ex) {
            console.log('Erreur de parsing')
        });
    },[]);

    const [stats, updateStats] = useState([]);
    const keys = stats.application
    
    console.log(keys)
    
    
    return(<div>
    </div>);
}

The moment i do console.log(keys), the content of the object is displayed correctly.

enter image description here

But when I do console.log(keys.name), the property name is not recognized.

log don't working

Why? For information, my getAdminApp function in the useEffect uses a GET on an API fetch. Thanks you in advance.

标签: javascriptreactjsjsx

解决方案


最后我做到了:

import React, {useEffect, useState} from "react";
import APIService from './service/ApiService'

export default function App() {

    useEffect(() => {
        APIService.getAdminApp('KYC').then(donnees => { //stats
            updateStats(donnees)
        }).catch(function (ex) {
            console.log('Erreur de parsing')
        });
    },[]);

    const [stats, updateStats] = useState([]);
    const keys = stats.application
    const array = stats.statistiqueList
    return(<div>
        <p>{keys?.name}</p>
        <p>{array[0]?.code}</p>
    </div>);
}

它正在工作,谢谢!^^


推荐阅读