Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将数据绑定到状态-React JS_Javascript_Reactjs - Fatal编程技术网

Javascript 将数据绑定到状态-React JS

Javascript 将数据绑定到状态-React JS,javascript,reactjs,Javascript,Reactjs,我正在为我的应用程序使用React。我正在使用Axios从API获取数据。 当我在console.log中记录response.data时,我可以在Google Chrome控制台中看到从API获取的数据 但是当我在console.log中记录“this.state.supplier”时,我在Google Chrome控制台中得到以下信息: API数据:[对象] 这是我的密码: class SupplierData extends React.Component{ constructor

我正在为我的应用程序使用React。我正在使用Axios从API获取数据。 当我在console.log中记录response.data时,我可以在Google Chrome控制台中看到从API获取的数据

但是当我在console.log中记录“this.state.supplier”时,我在Google Chrome控制台中得到以下信息:

API数据:[对象]

这是我的密码:

class SupplierData extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            supplier:[
                {
                    id: 0,
                    supplierTitle: "Supplier Title",
                    supplierFirstName: "First Name",
                    supplierLastName: "Last Name",
                    companyName: "Company Name",
                    phoneNumber: "Phone Number",
                    otherPhoneNumber: "Phone Number (Other)",
                    accountNumber: "Account Number",
                    email: "Email",
                    address: "Address",
                    website: "Website",
                    hourlyRate: "Hourly Rate",
                    typeOfGoods: "Type Of Goods",
                    paymentTerms: "Payment Terms",
                    createdAt: "Created At",
                    notes: "Notes",
                    products: "Products",
                    components: "Components"

                },

            ],
            errorMessage: [],
        };
        this.ListAllSuppliers = this.ListAllSuppliers.bind(this);
    }


    ListAllSuppliers = async () =>{
        return await axios.get(`http://localhost:8080/api/suppliers/supplier/list`)
            .then((response) =>{
                let results = response.data;
                console.log(results)
                this.setState({supplier: results})
                console.log(`API Data: ${this.state.supplier}`)
            }).catch((error) =>{
                this.setState({errorMessage: this.state.errorMessage.push(error)});
            })
    }
}


export default SupplierData;

为什么数据未绑定到状态?

发生这种情况是因为您试图将对象作为字符串进行控制台。字符串表示中的javascript对象是[object object]。尝试使用
console.log('API数据:',this.state.supplier)