Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.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 map不是react js中的函数_Javascript_Reactjs - Fatal编程技术网

Javascript map不是react js中的函数

Javascript map不是react js中的函数,javascript,reactjs,Javascript,Reactjs,试图映射此响应,但即使在我放入usestate数组时,它也向我显示了一个错误。请仔细检查我的代码,看看你是否能修复这个错误 function Comment() { const [comment_data, setcomment_data] = useState([]); useEffect(() => { const query = ` query{ forumAnswerId(id:1){ forumAnswerB

试图映射此响应,但即使在我放入
usestate
数组时,它也向我显示了一个错误。请仔细检查我的代码,看看你是否能修复这个错误

function Comment() {
    const [comment_data, setcomment_data] = useState([]);
    useEffect(() => {
        const query = `
    query{
        forumAnswerId(id:1){
        forumAnswerBody
        forumAnswerTime
        forumAnswerCode1
        forumAnswerCode2
        forumAnswerCode3
        forumAnswerAuthor
        forumAnswerBoolean
        forumAnswerCode1Title
        forumAnswerCode2Title
        forumAnswerCode3Title
        }
        forumComment(forumAnswerComment:1){
                forumAnswerCommentPost
                forumAnswerCommentBody
                forumAnswerCommentAuthor
                forumAnswerCommentTime  
        }
        }
`;
        const opts = {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ query }),
        };
        const res = fetch('http://127.0.0.1:8000', opts)
            .then((res) => res.json())
            .then((r) => setcomment_data(r));
    }, [])
    const map = comment_data.map((e) => {
        return (
            <>
                {comment_data.map((inner) => (
                    <>
                        {inner.data?.forumAnswerId?.map((data) => (
                            <></>
                        ))}
                    </>
                ))}
            </>
        );
    });
    console.log(comment_data);
    return (
        <div>
            {map}
        </div>
    );
}
函数注释(){
const[comment_data,setcomment_data]=useState([]);
useffect(()=>{
常量查询=`
质疑{
福鲁曼斯威德(身份证号码:1){
福鲁曼斯沃比
弗鲁曼斯瓦蒂姆
forumAnswerCode1
forumAnswerCode2
forumAnswerCode3
福鲁曼斯韦拉托尔
福鲁曼斯沃伯兰
forumAnswerCode1Title
forumAnswerCode2Title
forumAnswerCode3Title
}
论坛评论(论坛评论:1){
forumAnswerCommentPost
为人类共同体
forumAnswerCommentAuthor
对于我们来说,通常的时间
}
}
`;
常量选项={
方法:“POST”,
标题:{'Content-Type':'application/json'},
正文:JSON.stringify({query}),
};
常数res=fetch('http://127.0.0.1:8000",选择)
.然后((res)=>res.json())
。然后((r)=>setcomment_data(r));
}, [])
const map=comment_data.map((e)=>{
返回(
{comment_data.map((内部)=>(
{inner.data?.forumAnswerId?.map((数据)=>(
))}
))}
);
});
控制台日志(注释数据);
返回(
{map}
);
}
API结构


我使用了一个
useffect
钩子来加载数据,并将数据存储在state变量中,然后访问它。

如果我已经很好地理解了api的响应,那么您获取的不是数组。它是一个具有属性
data
的对象,这就是
comment\u data
中的内容。因此,没有
comment\u data.map()

您已记录并共享的comment\u数据显示comment\u数据是包含键“data”的对象

如果要映射该对象的所有键,可以执行object.keys(comment_数据),映射该数组并访问键的值,如comment_数据[key]

我认为,从代码的上下文来看,您正在寻找以下内容:

const map = comment_data.data?.forumAnswerId?.map((data) => (<>{data.forumAnswerBody}</>))
const map=comment\u data.data?.forumAnswerId?.map((data)=>({data.forumAnswerBody}))

也许可以告诉我们错误的确切位置?您是否尝试输出要调用的对象
.map
以查看它是否为数组?r数据的形状是什么?我已编辑,请查看它
数据
是否为对象?在这种情况下,您必须将其转换为数组进行迭代