【问题标题】:fetching data from API in react poll在反应轮询中从 API 获取数据
【发布时间】:2021-01-29 21:58:28
【问题描述】:

我想从 API 获取数据并使用 react 显示前端,但是我从前端收到错误(TypeError: answers.map is not a function )所以我该如何解决这个错误 --

我的代码是 -

import React, { useState, useEffect } from "react";
import Poll from "react-polls";
import { getPolls } from "../helper/coreapicalls";

const MainPoll = () => {
  const [polls, setPoll] = useState([]);
  const [error, seterror] = useState(false);
  // Setting answers to state to reload the component with each vote
  const [pollAnswers, setPollAnswers] = useState([]);

  useEffect(() => {
    loadPoll();
  }, []);

  const loadPoll = () => {
    getPolls().then((data) => {
      if (data.error) {
        seterror(data.error);
      } else {
        setPoll(data);
        console.log(data);
      }
    });
  };

  // Handling user vote
  // Increments the votes count of answer when the user votes
  const handalchange = () => {
    console.log("hello");
  };

  return (
    <div className="">
      <div className="container">
        <h1 className="blog_heading">Poll's of the Day</h1>
        <div className="row">
          {polls.map((poll, index) => (
            <div className="col-lg-4 col-12" key={index}>
              <Poll
                question={poll.question}
                answers={poll.options.none}
                onVote={handalchange}
              />
            </div>
          ))}
        </div>
      </div>
    </div>
  );
};

export default MainPoll;

我从 API 获得的数据是- 在这里我有问题,我如何向前端显示 3 个选项

错误 -

【问题讨论】:

    标签: node.js reactjs mongodb react-native express


    【解决方案1】:

    您向我们展示的代码中有两个小错误:

    1. 您导入的第一个 import Polls from "./polls"; 并调用 &lt;Poll noStorage question={poll.question} answers={poll.options} onVote={handleVote}/&gt; 只需更改 Poll by Polls。
    2. const [pollAnswers, setPollAnswers] = useState([...answers]); 这不起作用,因为您需要为您的状态传递一个初始值,而 answer 尚未初始化和访问。只需将 useState([...answers]); 更改为 useState([]);
      更新
      您需要将数组传递给 answers props 。 我们可以在您的控制台屏幕截图中看到,选项数组的键为“none”,因此 试试这个:&lt;Poll noStorage question={poll.question} answers={poll.options.none} onVote={handleVote}/&gt;(“none”是一个奇怪的键...)
      更新
      您的数据对象的格式不适合 react-polls 回答道具。 在react-polls 的 npmjs 文档中,我们可以看到一个选项示例,它是一个对象数组,如下所示:
    [
      { option: 'Yes', votes: 8 },
      { option: 'No', votes: 2 }
    ]
    

    因此,根据您在问题中添加的数据控制台日志,它应该如下所示:

    [
      {
        createdAt: "2020-12-01T21:43:23:21.061Z",
        options: {
          none: [ { option: 'Yes', votes: 8 },
          { option: 'No', votes: 2 }],
          student: ["12345678978945"],
          teacher: ["7894567894231"]
        },
        question: "Are you student ot teacher",
        updatedAt: "2020-12-01T21:43:23:21.061Z"
      }
    ]
    

    在此处查看sandBox 处理您的代码(getPolls() 除外)。 我认为问题来自 API。

    【讨论】:

    • 我更新了我的问题,但仍然出现错误
    • @Asitsingh 我已经“重新”更新了我的答案;-)
    • 谢谢,请回答这个问题 stackoverflow.com/q/66156615/15160538 @antoineso
    猜你喜欢
    • 1970-01-01
    • 2019-09-17
    • 1970-01-01
    • 2022-12-18
    • 2016-01-08
    • 2017-02-20
    • 2022-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多