我想实现此JSON结构:
{
"people": [
{
"name": "Peter",
"surname": "Green"
},
{
"name": "Jessica",
"surname": "Morgan"
},
...
}
我正在尝试这样做:
People({}).sort('-createdAt').exec(function(err, people) {
if(err) throw err;
console.log(people);
let data = {}
data['people'] = [];
data.push(people);
res.json(data);
});
当我查看生成的JSON结构时,其格式如下:
{
"people": [
[
{
"name": "Peter",
"surname": "Green"
},
...
在JSON中,people
节中有两个数组。如何从那里摆脱阵列之一?
谢谢
答案 0 :(得分:2)
我假设您正在从数据库中读取数据,看起来好像正在返回一堆人。
尝试不将其推入新阵列吗?
People({}).sort('-createdAt').exec(function(err, people) {
if(err) throw err;
console.log(people);
let data = {
people: people
};
res.json(data);
});