小编Pet*_*ulz的帖子

Response.json不显示值未定义的字段

Response.json没有显示带有值的字段 undefined

我正在实现一个节点api,以操纵Mongo数据库中的数据。查询使我享有权利JSON。当我将其发送JSON到React客户端时,包含的字段undefined不会显示。我究竟做错了什么?有解决方法吗?

为了缩小问题范围,请将以下代码添加到节点服务器。在console.log将打印右输出。例如status: undefined,它将包括,但res.json将不包括任何带有value的节点undefined

app.get("/test", function(req, res) {
    let test = {
        document: "test",
        data: {
            products: [
                {
                    id: 123,
                    type: "smartphone",
            name: "Motorola Moto G",
                    instock: true,
                    status: undefined,
                    link: null
                },
                {
                    id: 456,
                    type: "notsosmartphone",
            name: "Samsung S7",
                    instock: false,
                    status: "burning",
                    link: undefined
                }
            ],
            length: 2,
            date_created: '20161015T09:15',
            status: undefined,
            person: {name: "Kathy", age: 30, cute: true},
            arr:[123,"abc",{"a":"1"},{"b":"2"},{"c":"3"}]
        } …
Run Code Online (Sandbox Code Playgroud)

json node.js express

1
推荐指数
1
解决办法
1100
查看次数

标签 统计

express ×1

json ×1

node.js ×1