`values.map` 不是一个函数 reactjs.

huangapple go评论96阅读模式
英文:

values.map is not a function reactjs

问题

我正在从数据库中获取数据,想要将其显示在表格中,但最初我只想在浏览器中显示,而不是在表格中等等。但是我得到了一个错误 values.map is not a function,但我可以在控制台中看到值已经打印出来了。

这里是代码示例:

  1. export default function SimpleTable() {
  2. const [values, setValues] = useState([]);
  3. // 获取数据的函数
  4. async function handleTable() {
  5. const res = await fetch("http://localhost:4000/productslist");
  6. const data = await res.json();
  7. setValues(data.data);
  8. console.log(data.data);
  9. }
  10. // 在组件加载时调用获取数据的函数
  11. useEffect(() => {
  12. handleTable();
  13. }, []);
  14. // 在浏览器中渲染数据
  15. return (
  16. <div>
  17. {console.log(values)}
  18. {values.map(v => {
  19. return (
  20. <h4 key={v.idaddproducts}>
  21. {v.productName} {v.productId} {v.productBrand}
  22. </h4>
  23. );
  24. })}
  25. </div>
  26. );
  27. }

请注意,我将 useState 的初始值设置为一个空数组 [],因为您想要将数据映射到表格中,所以 values 应该是一个数组而不是对象。

英文:

i am fetchin details from database wanted to display that into the table, but for initial purpose i wanted to just display on browser without table and stuff.. am getting values.map is not a function but i could the see values printed in console

here iam using function component

  1. export default function SimpleTable() {
  2. const [values, setValues] = useState({});

here is the fetch function

  1. async function handleTable(){
  2. const res = await fetch(&quot;http://localhost:4000/productslist&quot;)
  3. const data = await res.json()
  4. setValues(data.data)
  5. console.log(data.data)
  6. }

calling the fetch function on useEffect

  1. useEffect(()=&gt;{
  2. handleTable()
  3. },[])

Rendering the values into browser

  1. return (
  2. &lt;div&gt;
  3. {console.log(values)}
  4. {values.map(v =&gt; {
  5. return &lt;h4 key={v.idaddproducts}&gt;{v.productName}{v.productId}{v.productBrand}&lt;/h4&gt;})}
  6. &lt;/div&gt;
  7. );
  8. }

here is the error

  1. Uncaught TypeError: values.map is not a function

答案1

得分: 3

The initial state of your values is an empty object

From

  1. const [values, setValues] = useState({});

Update to

  1. const [values, setValues] = useState([]);

Since Object doesn't have a method called .map

Also in your code since it's an async call, you can check based on a state whether data has loaded or not. A simple thing you can add is a ternary check:

  1. {values.length && values.map(v => {
  2. return <h4 key={v.idaddproducts}>{v.productName}{v.productId}{v.productBrand}</h4>;
  3. })}
  4. </div>
  5. );

So once the array has a length greater than zero, only then it will execute the map function.

Working Codesandbox With Sample Example

英文:

The initial state of your values is an empty object

From

  1. const [values, setValues] = useState({});

Update to

  1. const [values, setValues] = useState([]);

Since Object doesn't have a method called .map

Also in your code since its a async call you can check based on a state whether data has load or not a simple thing you can add is a ternary check

  1. {values.length &amp;&amp; values.map(v =&gt; {
  2. return &lt;h4 key={v.idaddproducts}&gt;{v.productName}{v.productId}{v.productBrand}&lt;/h4&gt;})}
  3. &lt;/div&gt;
  4. );

so once the array has a length greater than zero only it will execute the map function

Working Codesandbox With Sample Example

答案2

得分: 1

将您的状态定义为一个数组

  1. const [values, setValues] = useState([]);

代码

  1. {values && values.map(v => {
  2. return (
  3. <h4 key={v.idaddproducts}>
  4. {v.productName}{v.productId}{v.productBrand}
  5. </h4>
  6. );
  7. })}
英文:

Define your state as an array

  1. const [values, setValues] = useState([]);

Code

  1. {values &amp;&amp; values.map(v =&gt; {
  2. return &lt;h4 key={v.idaddproducts}&gt;
  3. {v.productName}{v.productId}{v.productBrand}
  4. &lt;/h4&gt;
  5. })
  6. }

答案3

得分: 0

你设置了一个值{}而不是[]
更新

  1. const [values, setValues] = useState({});

  1. const [values, setValues] = useState([]);
英文:

You set a value {} instead of [].
Update

  1. const [values, setValues] = useState({});

to

  1. const [values, setValues] = useState([]);

huangapple
  • 本文由 发表于 2020年1月6日 14:48:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/59607803.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定