gpt4 book ai didi

javascript - 如何在 nodejs 上使用对象 jSON 数据进行嵌套循环

转载 作者:行者123 更新时间:2023-11-30 11:15:40 25 4
gpt4 key购买 nike

我是 nodejs 和 mongodb 的新手,现在我使用 nodejs 和 mongodb 构建 restful API。我想为我的 API 使用响应标准 http://jsonapi.org标准。

我需要高级建议如何最好地做到这一点,使我的 API 响应类似于以下 JSON 数据:

HTTP/1.1 200 OK
Content-Type: application/vnd.api+json

{
"links": {
"self": "http://example.com/users"
},
"data": [{
"type": "users",
"id": 5b647bb8998248235a0aab3c,
"attributes": {
"username": "luke",
"email": "luke@mail.com",
"password": "password",
"hashpassword":"",
"oldpassword":"$2a$10$eyt6YV6m2JJrebNxvS0iEuxMCubDXratNJ6/XK797IGvepXBdp9Yq",
"salt":"2q6eN9U0vWFBsIF1MtB5WrgPiB8pldTS",
"usertype":"bisnis",
"userstatus":"not aktif"
}
}, {
"type": "users",
"id": 5b647bdf998248235a0aab3d,
"attributes": {
"username": "ken",
"email": "ken@mail.com",
"password": "password",
"hashpassword":"",
"oldpassword":"$2a$10$eyt6YV6m2JJrebNxvS0iEuxMCubDXratNJ6/XK797IGvepXBdp9Yq",
"salt":"2q6eN9U0vWFBsIF1MtB5WrgPiB8pldTS",
"usertype":"bisnis",
"userstatus":"not aktif"
}
}]
}

如上创建输出时,嵌套迭代出现问题。这是我的代码:

const UsersModel = mongoose.model('Users');

//...show list user
exports.listUsers = (req, res) => {
UsersModel.find({}, (err, result) => {
if (err) {
res.send({code: 400, failed: 'Error 400'});
}
res.json(result);
});
};

这是我的结果 JSON:

[
{
"type": "users",
"userstatus": "not aktif",
"_id": "5b647bb8998248235a0aab3c",
"username": "luke",
"email": "luke@mail.com",
"password": "password",
"usertype": "bisnis",
"hashpassword": "$2a$10$eyt6YV6m2JJrebNxvS0iEuxMCubDXratNJ6/XK797IGvepXBdp9Yq",
"salt": "2q6eN9U0vWFBsIF1MtB5WrgPiB8pldTS",
"__v": 0
},
{
"type": "users",
"userstatus": "tidak aktif",
"_id": "5b647bdf998248235a0aab3d",
"username": "ken",
"email": "ken@mail.com",
"password": "password",
"usertype": "personal",
"hashpassword": "$2a$10$hok988mszyIBiXVNjmfifOiPNzXkBRRRynXJS/0qCkvlaBOQs65MO",
"salt": "IiMvtVYVqTpZFXmYQIM4IlS6PJFVZ3kw",
"__v": 0
}
]

这是我解决问题的临时代码。

//...show list user
exports.listUsers = (req, res) => {
UsersModel.find({}, (err, result) => {
if (err) {
res.send({code: 400, failed: 'Error 400'});
}

let listData = [];

for (let key in result) {
let data = {};
let attr = {};

if (result.hasOwnProperty(key)) {
data.type = result[key].type;
data.id = result[key]._id;

for(let i in result[key]) {

if(result[key].hasOwnProperty(i)) {
attr.username = result[key].username;
attr.email = result[key].email;
attr.password = result[key].password;
attr.hashpassword = result[key].hashpassword;
attr.oldpassword = result[key].oldpassword;
attr.salt = result[key].salt;
attr.usertype = result[key].usertype;
attr.userstatus = result[key].userstatus;
}
}

data.attribute = attr;
listData.push(data);
}

}

let collections = {
"meta": {
"copyright": "Copyright 2018 Kotakku Studio and Lab",
"authors": [
"sw. saputra"
]
},
"link": {
"self": req.protocol + '://' + req.get('host') + req.originalUrl
},
"data": listData
}

res.json(collections);
});
};

如果我的临时代码不正确,请给我建议解决问题的优雅和最佳方法。

提前致谢。

最佳答案

您需要根据您的要求创建一个合适的 mongoose 模式并将资源映射到该模式。

//Schema for users
var UsersSchema = mongoose.Schema({
links: {
self: String
},
data:[{
type: String,
attributes: {
username: String,
.
.
.
}
}]
})

数组中每个文档的 id 将由 MongoDB 自动创建。对于 Mongoose 模式文档,请参阅 http://mongoosejs.com/docs/guide.html您还可以查看这个简单的 TODO API 以供引用 https://github.com/mkujaggi/node-course-todo-api

关于javascript - 如何在 nodejs 上使用对象 jSON 数据进行嵌套循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51686347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com