简体   繁体   English

错误:对象在 reactjs 中作为 React 子对象无效(找到:[object Promise])

[英]Error: Objects are not valid as a React child (found: [object Promise]) in reactjs

I'm learning reactjs.我正在学习 reactjs。 I want to get result from fuction findTenloaidv(id) when load list bpdvList but error我想在加载列表 bpdvList 时从函数 findTenloaidv(id) 中获取结果但错误

    async componentDidMount(){
        this.setState({isLoading: true});
        const bangphi = await (await fetch(`/gvnhanh/bangphidv/`)).json();
        this.setState({
                bpdvs: bangphi,
                isLoading: false,
            })
    }

    async findTenloaidv(id){
        const loaidv = await (await fetch(`/gvnhanh/loaidv/${id}`)).json();
        
        return loaidv.tenloai
    }

render(){

        const {bpdvs, isLoading} = this.state;

        const bpdvList = bpdvs.map(bpdv =>{
            return <tr key={bpdv.idbpdv}>
                        <td className="text-center">{bpdv.iddv}</td>
                        <td className="text-center">{this.findTenloaidv(bpdv.idloaidv)}</td>
                        <td className="text-center">{bpdv.tendv}</td>
                        <td className="text-center">{bpdv.mota}</td>
                        <td className="text-center">{this.currencyFormat(bpdv.gia)}</td>
                        <td className="text-center">{bpdv.donvitinh}</td>
                    </tr>

        });
}

There are a couple errors here.这里有几个错误。

async componentDidMount(){
    this.setState({isLoading: true});
    const bangphi = await (await fetch(`/gvnhanh/bangphidv/`)).json(); // Here
    const bangphi = await fetch('Your endpoint') //await your fetch data
    const data = await bangphi.json()  // Then convert to json
    this.setState({
            bpdvs: data.bangphi, // changed here
            isLoading: false,
        })
}
// Change this to match 
async findTenloaidv(id){
    const loaidv = await (await fetch(`/gvnhanh/loaidv/${id}`)).json();
    
    return loaidv.tenloai
}

The problem is here问题就在这里

<td className="text-center">{this.findTenloaidv(bpdv.idloaidv)}</td>

findTenloaidv method returns a promise as it is async which is type of object ([object Promise]) . findTenloaidv方法返回一个promise因为它是async ,它是对象([object Promise]) That's why react throws this error Objects are not valid as a React child .这就是为什么 react 会抛出这个错误Objects are not valid as a React child

You need to map the currency format while you're fetching data.您需要在获取数据时映射货币格式。 Try like this像这样尝试

async componentDidMount(){
    this.setState({isLoading: true});
    const bangphi = await (await fetch(`/gvnhanh/bangphidv/`)).json(); // Here
    const bangphi = await fetch('Your endpoint') //await your fetch data
    const data = await bangphi.json()  // Then convert to json
    const bpdvs = data.bangphi.map(async bpdv => {
       const loaidv = await this.findTenloaidv(bpdv.idloaidv)
       return {
         ...bpdv,
         idloaidv: loaidv.tenloai
       }
    })
    this.setState({
            bpdvs,
            isLoading: false,
        })
}
   ...
    <td className="text-center">{bpdv.iddv}</td>
    <td className="text-center">{bpdv.idloaidv}</td>
   ....

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React 错误“对象作为 React 子项无效(找到:[object Promise])” - React error "Objects are not valid as a React child (found: [object Promise])" 我收到一个错误:对象作为 React 子对象无效(找到:[object Promise]) - I got an Error: Objects are not valid as a React child (found: [object Promise]) 如何修复错误:对象作为 React 子对象无效(找到:[object Promise]) - How to fix Error: Objects are not valid as a React child (found: [object Promise]) 对象作为 React 子级无效(发现:[object Promise]) - Objects are not valid as a React child (found: [object Promise]) 对象无效,无法作为React子对象(找到:[object Promise]) - Objects not valid as a React child (found: [object Promise]) 对象在反应渲染中作为 React 子级无效(找到:[object Promise]) - Objects are not valid as a React child (found: [object Promise]) in react render 无法在 JSX 中渲染对象。 抛出错误对象作为 React 子对象无效(发现:[object Promise]) - Can't render Objects inside JSX. Throwing error Objects are not valid as a React child (found: [object Promise]) React 中的异步函数触发错误:对象作为 React 子对象无效(找到:[object Promise]) - Async function in React triggers error: Objects are not valid as a React child (found: [object Promise]) Redux:对象作为 React 子项无效(找到:[object Promise]) - Redux: Objects are not valid as a React child (found: [object Promise]) 对象作为尝试返回 swal 的 React 子项(找到:[object Promise])无效 - Objects are not valid as a React child (found: [object Promise]) trying to return swal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM