繁体   English   中英

从 Nodejs 向前端发送数据

[英]Sending data to the frontend from Nodejs

我正在格式化一些数据以提高可读性。

我正在将这些数据发送到前端,看

代码

res.status(200).json({dealersData});

dealersData是一个 json 包含

{
  "Dealers":{
    "Detail":[
      {
        "Table":[
          {
            "DealerId":[
              "1"
            ],
            "DealerName":[
              "Carmen"
            ],
            "NickName":[
              "Carmen"
            ],
            "Picture":[
              "00001.jpg"
            ],
            "Active":[
              "1"
            ],
            "LegalId":[
              "111111111"
            ],
            "TypeId":[
              "1"
            ]
          }
        ]
      }
    ]
  }
}

所以我需要对Lodash做的是将数据发送到前端而没有 '"Dealers":{...} and without the '"Detail":[...]' part, I need to send it starting the JSON from “表”的 JSON`

让我们这样说

      {
        "Table":[
          {
            "DealerId":[
              "1"
            ],
            "DealerName":[
              "Carmen"
            ],
            "NickName":[
              "Carmen"
            ],
            "Picture":[
              "00001.jpg"
            ],
            "Active":[
              "1"
            ],
            "LegalId":[
              "111111111"
            ],
            "TypeId":[
              "1"
            ]
          }

我已经试过了,但我得到了类似的东西

dealersData {[[null]]}

那么,您有什么建议?

res.status(200).json(dealersData.Dealers.Detail[0]); 

访问 Dealers 键,然后是 Detail 数组中的第一个对象

只需将您的回复更改为:

res.status(200).json(dealersData.Dealers.Detail[0]);

也许检查数据有效性:

if(dealersData && dealersData.Dealers && dealersData.Dealers.Detail) {
    res.status(200).json(dealersData.Dealers.Detail[0]);
} else {
    res.status(404).json({'success':false});
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM