NextJS -防止NextResponse向JSON结果添加根元素

ruarlubt  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(125)

我在NextJs中创建了一个API,NextResponse将根元素添加到生成的JSON中。
下面是API的返回值:

return NextResponse.json({result},{ status: 200 });

字符串
下面是调用的原始结果。

[{"id":11,"first_name":"test2","last_name":"employee","job_title":"test job","employeeStrengths":[{"id":65,"order":1,"strength":{"title":"Achiever"}},{"id":580,"order":2,"strength":{"title":"Ideation"}},{"id":581,"order":3,"strength":{"title":"Futuristic"}},{"id":582,"order":4,"strength":{"title":"Relator"}},{"id":583,"order":5,"strength":{"title":"Learner"}},{"id":66,"order":6,"strength":{"title":"Competition"}},{"id":579,"order":7,"strength":{"title":"Strategic"}},{"id":578,"order":8,"strength":{"title":"Analytical"}}]}]


以下是API(NextResponse)返回的内容:

{
    "result": [
        {
            "id": 11,
            "first_name": "test2",
            "last_name": "employee",
            "job_title": "test job",
            "employeeStrengths": [
                {
                    "id": 65,
                    "order": 1,
                    "strength": {
                        "title": "Achiever"
                    }
                },
                {
                    "id": 580,
                    "order": 2,
                    "strength": {
                        "title": "Ideation"
                    }
                },
                {
                    "id": 581,
                    "order": 3,
                    "strength": {
                        "title": "Futuristic"
                    }
                },
                {
                    "id": 582,
                    "order": 4,
                    "strength": {
                        "title": "Relator"
                    }
                },
                {
                    "id": 583,
                    "order": 5,
                    "strength": {
                        "title": "Learner"
                    }
                },
                {
                    "id": 66,
                    "order": 6,
                    "strength": {
                        "title": "Competition"
                    }
                },
                {
                    "id": 579,
                    "order": 7,
                    "strength": {
                        "title": "Strategic"
                    }
                },
                {
                    "id": 578,
                    "order": 8,
                    "strength": {
                        "title": "Analytical"
                    }
                }
            ]
        }
    ]
}


我想去掉根元素“result:“
任何见解都值得赞赏。

js5cn81o

js5cn81o1#

不要破坏它。

return NextResponse.json(result,{ status: 200 });

字符串
还有一件事,status: 200是默认值。你可以把它去掉。

return NextResponse.json(result)

相关问题