React 中的 JSON 到 Dropdown - response.data.map 不是函数
我正在关注代码: https://codesandbox.io/s/cranky-leaf-2hupv?fontsize=14&hidenavigation=1&module=%2Fsrc%2FApp.js
该示例在我的 React 项目中有效,但是当我尝试时为了使其适应我数据库中的值,我收到错误:
Uncaught (in Promise) TypeError: response.data.map is not a function
我的 JSON 很简单
categories:
0:
Id: "22"
Name: "Strategy"
1:
Id: "19"
Name: "Sports"
2:
Id: "27"
Name: "Branding"
我已经调整了代码以适合我的JSON 如下:
import React, { Component } from 'react';
import axios from 'axios';
export default class TestDropdown extends React.Component {
constructor(props) {
super(props);
this.toggle = this.toggle.bind(this);
this.state = {
display: "",
titles: [],
errorMsg: ''
};
}
toggle() {
this.setState(prevState => ({
dropdownOpen: !prevState.dropdownOpen
}));
}
componentDidMount = (e) => {
axios.get("https://mysite/devapi/categories.php").then((response) =>
this.setState({
titles: response.data.map(({ Name }) => Name), /*error*/
display:
response.data[Math.floor(Math.random() * response.data.length)].title
})
);
};
render() {
const { display, titles } = this.state;
return (
<div className="container">
<select defaultValue={display}>
{titles.map((Name) => (
<option key={Name} value={Name}>
{Name}
</option>
))}
</select>
</div>
);
}
}
我考虑过我是否因不访问“类别”而读取了错误的 JSON 值而犯了错误,或者这是否与地图/列表有关。 我只需要名称值来填充选项。
来自初学者的衷心感谢。
I am following the code: https://codesandbox.io/s/cranky-leaf-2hupv?fontsize=14&hidenavigation=1&module=%2Fsrc%2FApp.js
The example works in my React project but when I try to adapt it to values from my database, I get the error:
Uncaught (in promise) TypeError: response.data.map is not a function
My JSON is simple
categories:
0:
Id: "22"
Name: "Strategy"
1:
Id: "19"
Name: "Sports"
2:
Id: "27"
Name: "Branding"
I have adapted the code to fit my JSON as follows:
import React, { Component } from 'react';
import axios from 'axios';
export default class TestDropdown extends React.Component {
constructor(props) {
super(props);
this.toggle = this.toggle.bind(this);
this.state = {
display: "",
titles: [],
errorMsg: ''
};
}
toggle() {
this.setState(prevState => ({
dropdownOpen: !prevState.dropdownOpen
}));
}
componentDidMount = (e) => {
axios.get("https://mysite/devapi/categories.php").then((response) =>
this.setState({
titles: response.data.map(({ Name }) => Name), /*error*/
display:
response.data[Math.floor(Math.random() * response.data.length)].title
})
);
};
render() {
const { display, titles } = this.state;
return (
<div className="container">
<select defaultValue={display}>
{titles.map((Name) => (
<option key={Name} value={Name}>
{Name}
</option>
))}
</select>
</div>
);
}
}
I have considered whether I am making a mistake by reading the wrong JSON value by not accessing 'categories' or whether this has something to do with map / list.
I would only need the Name values to populate the Option with.
Kind thanks from a beginner.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试执行类似
或 的
操作来确保我们拥有该数据
玩得开心!
You can try do something like
or
To make sure, that we have that data
Have fun!
通过检查响应的长度response.data.map不是函数错误将被删除
By checking length of response response.data.map is not a function error will be removed