Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

148
Views
Secuela nodejs devuelve JSON plano

Estoy usando Sequelize con nodejs para una API RESTful. Tengo 2 modelos, Seguridad y Ordenes. El modelo de pedido hace referencia al modelo de seguridad utilizando una clave externa a través de un símbolo.

Modelo de seguridad:

 classMethods: { associate: function(models) { // associations security.hasMany(models.order, { foreignKey: 'security_symbol' }); } }

Modelo de pedido:

 classMethods: { // associations associate: function(models) { order.belongsTo(models.security, { foreignKey: 'security_symbol' }) } }

Luego realizo una consulta para devolver todos los pedidos que funcionan bien, aunque quiero que se incluyan otros atributos del modelo de seguridad en una especie de json 'aplanado'. Consulta:

 Order.findAndCountAll({ where: { user_id: user.id}, attributes: [ 'id', 'side', 'quantity', 'price', 'status', 'created_at' ], order: [ ['id', 'DESC'] ], include: [{ model: models.security, attributes: ['name', 'symbol'] }] })

Respuesta:

 "orders": [ { "id": 25, "side": 1, "quantity": 1150, "price": "13.33", "status": 0, "created_at": "2017-04-27T09:51:41.479Z", "security": { "name": "Car & General (K) Ltd", "symbol": "C&G" } }, { "id": 24, "side": 1, "quantity": 1000, "price": "5.63", "status": 4, "created_at": "2017-04-27T09:50:31.939Z", "security": { "name": "Eveready East Africa Ltd", "symbol": "EVRD" } }...

Respuesta deseada:

 "orders": [ { "id": 25, "side": 1, "quantity": 1150, "price": "13.33", "status": 0, "created_at": "2017-04-27T09:51:41.479Z", "security_name": "Car & General (K) Ltd", "security_symbol": "C&G" }, { "id": 24, "side": 1, "quantity": 1000, "price": "5.63", "status": 4, "security_name": "Eveready East Africa Ltd", "security_symbol": "EVRD" "created_at": "2017-04-27T09:50:31.939Z", }...

La diferencia es que el 'padre' no está anidado dentro del pedido.

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Puede usar el atributo raw como se muestra a continuación

 Order.findAndCountAll({ where: { user_id: user.id}, attributes: [ 'id', 'side', 'quantity', 'price', 'status', 'created_at' ], order: [ ['id', 'DESC'] ], include: [{ model: models.security, attributes: ['name', 'symbol'] }] raw: true })

Pero luego la secuencia te dará un resultado como

 "orders": [ { "id": 25, "side": 1, "quantity": 1150, "price": "13.33", "status": 0, "created_at": "2017-04-27T09:51:41.479Z", "security.name": "Car & General (K) Ltd", "security.symbol": "C&G" }, { "id": 24, "side": 1, "quantity": 1000, "price": "5.63", "status": 4, "security.name": "Eveready East Africa Ltd", "security.symbol": "EVRD" }...

No estoy seguro de si security.name & security.symbol es deseable o no. Pero así es como se comportará Sequelize. Espero que esto ayude

about 4 years ago · Santiago Trujillo Report

0

Prueba lo siguiente:

 Order.findAndCountAll({ where: { user_id: user.id}, attributes: [ 'id', 'side', 'quantity', 'price', 'status', 'created_at', [Sequelize.literal('"order->security".name'), 'security_name'], [Sequelize.literal('"order->security".symbol'), 'security_symbol'] ], order: [ ['id', 'DESC'] ], include: [{ model: models.security, attributes: []}] })

Tenga en cuenta que es posible que deba actualizar order->security a los nombres reales de la tabla DB ( orders y securities )

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!