Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays React-映射嵌套的json,不在数组中_Arrays_Json_Reactjs_Dictionary - Fatal编程技术网

Arrays React-映射嵌套的json,不在数组中

Arrays React-映射嵌套的json,不在数组中,arrays,json,reactjs,dictionary,Arrays,Json,Reactjs,Dictionary,我正在尝试映射一些json数据,看起来像这样 "array": [ { "controlPanel": "00000000-0000-0000-0000-000000000000", "lastArmed": "2016-10-31T19:36:19.3943138", "lastDisarmed": "2017-02-04T22:50:47.3528838", "panelDateT

我正在尝试映射一些json数据,看起来像这样

"array": [
        {
            "controlPanel": "00000000-0000-0000-0000-000000000000",
            "lastArmed": "2016-10-31T19:36:19.3943138",
            "lastDisarmed": "2017-02-04T22:50:47.3528838",
            "panelDateTime": "2019-10-14T09:00:31.0908467",
            "mainsConnected": false,
            "mainsLastActivated": "2017-04-19T04:53:47.9033877",
            "tamperStatus": "0",
            "activeFaults": "TwoW, AC, BP, DF, LV, NF, OP, SR",
            "battery": {
                "voltage": 0.0,
                "current": 0
            },
            "system": {
                "voltage": 6.515163,
                "current": 1547
            },
            "aux12V": {
                "voltage": 2.767401,
                "current": 119
            },
            "bell": {
                "voltage": 14.019639,
                "current": 405
            },
            "network": null,
            "temperature": 68.538864,
            "id": "fd14f8da-2175-e75d-e2a2-00647f9d3884"
        },
class ControlPanelDetail extends React.Component {
    constructor(props) {
        super(props);
        this.timeIncrementMs = 50;
        this.showSpinnerIfReturnGreaterThanMs = 200;
        this.state = {
            loading: false,
            controlPanel: [],
            controlPanelStatus: [
                {
                    "battery": {
                        "voltage": '',
                        "current": ''
                    },
                    "system": {
                        "voltage": '',
                        "current": ''
                    },
                    "aux12V": {
                        "voltage": '',
                        "current": ''
                    },
                    "bell": {
                        "voltage": '',
                        "current": ''
                    },
                }
            ],
            toggledClearRows: false
        }
    }
fetch('/api/controlpanelstatus')
    .then(res => res.json())
    .then(cp => {
        this.setState({
            controlPanelStatus: cp.array[0],
            isLoading: false
        });
        console.log(this.state.controlPanelStatus)
    })
    .catch(error => {
        if (error.response) {
            console.log(error.responderEnd);
        }
    });
我一直得到TypeError:执行此操作时无法读取未定义的属性“map”

                        {this.state.controlPanelStatus.battery.map((b, bix) => (
                        <tr key={bix}>
                            <th>Battery Voltage (V)</th>
                            <td>{b.voltage}</td>
                            <td>Pass</td>
                            <td>10.5V&lt;&gt;14.5V</td>
                        </tr>
                        ))}
我正在从api加载第一个条目,如下所示

"array": [
        {
            "controlPanel": "00000000-0000-0000-0000-000000000000",
            "lastArmed": "2016-10-31T19:36:19.3943138",
            "lastDisarmed": "2017-02-04T22:50:47.3528838",
            "panelDateTime": "2019-10-14T09:00:31.0908467",
            "mainsConnected": false,
            "mainsLastActivated": "2017-04-19T04:53:47.9033877",
            "tamperStatus": "0",
            "activeFaults": "TwoW, AC, BP, DF, LV, NF, OP, SR",
            "battery": {
                "voltage": 0.0,
                "current": 0
            },
            "system": {
                "voltage": 6.515163,
                "current": 1547
            },
            "aux12V": {
                "voltage": 2.767401,
                "current": 119
            },
            "bell": {
                "voltage": 14.019639,
                "current": 405
            },
            "network": null,
            "temperature": 68.538864,
            "id": "fd14f8da-2175-e75d-e2a2-00647f9d3884"
        },
class ControlPanelDetail extends React.Component {
    constructor(props) {
        super(props);
        this.timeIncrementMs = 50;
        this.showSpinnerIfReturnGreaterThanMs = 200;
        this.state = {
            loading: false,
            controlPanel: [],
            controlPanelStatus: [
                {
                    "battery": {
                        "voltage": '',
                        "current": ''
                    },
                    "system": {
                        "voltage": '',
                        "current": ''
                    },
                    "aux12V": {
                        "voltage": '',
                        "current": ''
                    },
                    "bell": {
                        "voltage": '',
                        "current": ''
                    },
                }
            ],
            toggledClearRows: false
        }
    }
fetch('/api/controlpanelstatus')
    .then(res => res.json())
    .then(cp => {
        this.setState({
            controlPanelStatus: cp.array[0],
            isLoading: false
        });
        console.log(this.state.controlPanelStatus)
    })
    .catch(error => {
        if (error.response) {
            console.log(error.responderEnd);
        }
    });
所有数据都从我试图映射的嵌套json正确加载。 我做错了什么?这是因为json没有嵌套为数组例如[]吗?
谢谢。

我在
.map()
函数中更改了您的代码。我所做的只是在运行时处理异常

{
this.state.controlPanelStatus&&
this.state.controlPanelStatus.map((b,bix)=>(
// ...
));
}

我在
.map()函数中更改了您的代码。我所做的只是在运行时处理异常

{
this.state.controlPanelStatus&&
this.state.controlPanelStatus.map((b,bix)=>(
// ...
));
}


controlPanelStatus
是一个数组,它不包含属性
battery
@PraveenKumarPurushothaman我已更新了代码中的状态。像这样的?仍然得到相同的错误。TypeError:无法读取未定义的属性“map”,但您仍然可以看到它是一个数组。渲染代码错误。您必须映射每个
控制面板状态
项才能获得
电池
controlPanelStatus
是一个包含
battery
值的对象数组。能否创建一个CodeSandbox.io以显示该模拟状态?我可以帮你找到正确的密码,像这样?{this.state.controlPanelStatus.map((b,bix)=>(电池电压(V){b.Battery.Voltage}通过{b.电池电流}
controlPanelStatus
是一个数组,它不包含属性
battery
@PraveenKumarPurushothaman我已经更新了代码中的状态。类似这样的情况?仍然会收到相同的错误。TypeError:无法读取未定义染料的属性“map”,仍然可以看到它是一个数组。Yo你的呈现代码是错误的。你必须映射每个
controlPanelStatus
项才能获得
battery
值。
controlPanelStatus
是一个包含
battery
值的对象数组。你能创建一个CodeSandbox.io来显示模拟状态吗?我可以帮你找到正确的代码。像这样吗?{this.state.controlPanelStatus.map((b,bix)=>(电池电压(V){b.Battery.Voltage}传递{b.battery.current}这现在可以工作,但只有在我更改controlPanelStatus:cp.array[0]时才能工作,到controlPanelStatus:cp.array。我只是想用api中的第一个条目进行测试,但看起来map函数只有在从api加载所有数据时才起作用。我想我必须传入id才能正确执行此操作。这可能是个问题……但我的一个建议是,请尽可能将代码拆分为一个单元……现在,所有内容都在一个单个文件。@StevenCollins还让我知道您是否需要任何进一步的帮助。很高兴帮助您。我想知道您是否可以帮助解决此路由/快速服务器问题?谢谢。@StevenCollins Lemme check。此功能现在可用,但仅当我更改controlPanelStatus:cp.array[0],到controlPanelStatus:cp.array。我只是想用api中的第一个条目进行测试,但看起来map函数只有在从api加载所有数据时才起作用。我想我必须传入id才能正确执行此操作。这可能是个问题……但我的一个建议是,请尽可能将代码拆分为一个单元……现在,所有内容都在一个单个文件。@StevenCollins还告诉我您是否需要任何进一步的帮助。很高兴帮助您。我想知道您是否可以帮助解决这个路由/快递服务器问题?谢谢。@StevenCollins Lemme检查。