【问题标题】:Write and read nested objects in a database in React [duplicate]在 React 中的数据库中写入和读取嵌套对象 [重复]
【发布时间】:2020-03-24 22:06:14
【问题描述】:

我正在尝试从我的数据库中获取嵌套项,但不知道如何。

我的数据库看起来像:

GET /dwelling/room/


[
    {
        "room_id": 1,
        "room_name": "Living Room",
        "room_data": [
            {
                "id": 1,
                "temperature": "5,15,9"
                ...

我目前正在使用:

    fetch('https://.../dwelling/room/', {
      method: 'GET',
    }).then(resp => resp.json())
      .then(resp => this.setState({room: resp}))
      .catch(error => console.log(error))
  }

从数据库中获取数据以及

{props.room.map(room => {

                return (

                    <React.Fragment>
                        <div>{room.room_name}<div>
                    </React.Fragment>
                    ...

我能够获取 room_id 和 room_name 的数据,但无法从 room_data 中获取任何内容。我希望能够将温度放入图表中,但我什至无法将数据打印在列表中。我将不胜感激任何帮助!谢谢!

【问题讨论】:

    标签: javascript html reactjs


    【解决方案1】:

    根据您的记录判断,您只需访问room.room_data[0].temperature即可获得室温

    [
        {
            "room_id": 1,
            "room_name": "Living Room",
            "room_data": [
                {
                    "id": 1,
                    "temperature": "5,15,9"
                    ...
    

    【讨论】:

    • 请不要忘记支持这个答案..
    猜你喜欢
    • 2017-04-04
    • 2019-05-02
    • 2018-10-12
    • 2018-11-22
    • 2020-12-11
    • 1970-01-01
    • 2017-09-29
    • 2021-06-08
    • 2019-10-22
    相关资源
    最近更新 更多