gpt4 book ai didi

javascript - 更新 json 以显示 json 数组中的对象计数,而不是 Node 应用程序中的列表

转载 作者:行者123 更新时间:2023-12-01 01:00:52 26 4
gpt4 key购买 nike

我有这个 json 结构:

    {
"sales_rep": {
"1": {
"id": 1,
"name": "Joe",
"email": "joebloggs@email.com",
"customers": [
{
"id": 1,
"address": "1 High Street",
"name": "CUSTOMER1",
"supplierId": 1,
},
{
"id": 2,
"address": "2 High Street",
"name": "CUSTOMER2",
"supplierId": 1,
},
{
"id": 3,
"address": "3 High Street",
"name": "CUSTOMER3",
"supplierId": 1
},
]
}
}
}

是否可以编写一个函数来提供客户计数而不是客户详细信息?我希望最终得到这样的结果:

    {
"sales_rep": {
"1": {
"id": 1,
"name": "Joe",
"email": "joebloggs@email.com",
"customers": "3"
}
}
}

我想我需要某种reduce函数,但我不知道该怎么做。

我遇到的主要问题之一是原始 json 是使用此 Sequelize 模型生成的:

let users = db.SalesRep.findAll({
attributes: ['id', 'name', 'email'],
include: [{
model: db.Customers,
as: 'customers',
}],
where: {'id': "1"},
order: orderClause,
offset,
limit,
});

如果我添加 console.log(users) 我会得到:

Promise {
_bitField: 0,
_fulfillmentHandler0: undefined,
_rejectionHandler0: undefined,
_promise0: undefined,
_receiver0: undefined,
_trace:
{ Error
at Promise.longStackTracesCaptureStackTrace [as _captureStackTrace] (/Users/paulcarron/git/Eighty8/updated-api/node_modules/bluebird/js/release/debuggability.js:411:19)
at Promise._then (/Users/paulcarron/git/Eighty8/updated-api/node_modules/bluebird/js/release/promise.js:232:17)
at Promise.then (/Users/paulcarron/git/Eighty8/updated-api/node_modules/bluebird/js/release/promise.js:125:17)
at Function.findAll (/Users/paulcarron/git/Eighty8/updated-api/node_modules/sequelize/lib/model.js:1748:8)
at Function.User.listForAdmin (/Users/paulcarron/git/Eighty8/updated-api/app/models/User.js:65:25)
at router.get (/Users/paulcarron/git/Eighty8/updated-api/app/controllers/admin.js:277:35)
at Layer.handle [as handle_request] (/Users/paulcarron/git/Eighty8/updated-api/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/paulcarron/git/Eighty8/updated-api/node_modules/express/lib/router/route.js:137:13)
at /Users/paulcarron/git/Eighty8/updated-api/app/middlewares/role.js:6:12
at Layer.handle [as handle_request] (/Users/paulcarron/git/Eighty8/updated-api/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/paulcarron/git/Eighty8/updated-api/node_modules/express/lib/router/route.js:137:13)
at cognitoExpress.validate (/Users/paulcarron/git/Eighty8/updated-api/app/middlewares/access.js:25:7) _parent: undefined, _promisesCreated: 0, _length: 1 } }

最佳答案

findAll() 是一个异步函数,它返回一个 Promise。您需要使用 .then() 来获取结果并在那里执行转换。

db.SalesRep.findAll({
attributes: ['id', 'name', 'email'],
include: [{
model: db.Customers,
as: 'customers',
}],
where: {'id': "1"},
order: orderClause,
offset,
limit,
}).then(function(users) {
let sales_rep = users.sales_rep;
for (let key in sales_rep) {
sales_rep[key].customers = sales_rep[key].customers.length;
}
// do more stuff with users
});

关于javascript - 更新 json 以显示 json 数组中的对象计数,而不是 Node 应用程序中的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56089808/

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