React 中的 JSON 到 Dropdown - response.data.map 不是函数

发布于 2025-01-09 08:57:53 字数 1990 浏览 0 评论 0原文

我正在关注代码: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

澉约 2025-01-16 08:57:53

您可以尝试执行类似

response?.data?.map(({ Name }) => Name)

或 的

response && responese.data && response.data.map((element, index) => {
   console.log('My data', element) // here you should see your data
})

操作来确保我们拥有该数据

玩得开心!

You can try do something like

response?.data?.map(({ Name }) => Name)

or

response && responese.data && response.data.map((element, index) => {
   console.log('My data', element) // here you should see your data
})

To make sure, that we have that data

Have fun!

凯凯我们等你回来 2025-01-16 08:57:53

通过检查响应的长度response.data.map不是函数错误将被删除

response.data.length > 0 && response.data.map((element, index) => {
console.log('My data', element) // here you should see your data
})

By checking length of response response.data.map is not a function error will be removed

response.data.length > 0 && response.data.map((element, index) => {
console.log('My data', element) // here you should see your data
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文