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

484
Views
Nodejs Sequelize Attributes Obtener datos dentro de un objeto

Tengo una base de datos de postgres que se ve así:

ingrese la descripción de la imagen aquí

Quiero consultar esos datos y devolver solo algunos de ellos. A continuación se muestra la función que escribí.

ciudadanoControlador.js

 exports.getAllCitizen = async (req, res) => { const { page, size } = req.query; //Check if theres a NIK filter const nik = req.query.nik; const condition = nik ? { nik: { [Op.iLike]: `%${nik}%` } } : null; //Pagination const { limit, offset } = getPagination(page, size); try { const response = await Penduduk.findAndCountAll({ where: condition, attributes: ["id", "nik", "profil_penduduk"], limit, offset, }); const data = getPagingData(response, page, limit); return res.status(200).send(data); } catch (err) { console.log(err); } };

Aquí está la respuesta de la función anterior:

ingrese la descripción de la imagen aquí

El problema es que no quiero devolver todos los datos del objeto profil_penduduk , solo quiero obtener nama , alamat y status_warga de él.

ingrese la descripción de la imagen aquí

¿Cómo puedo lograr eso? Traté de hacer como:

 attributes: ["id", "nik", "profil_penduduk.nama","profil_penduduk.alamat","profil_penduduk.status_warga"]

Pero no funcionó.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

1

La mejor manera de obtener los detalles de la tabla de referencia para usar include como

 const response = await Penduduk.findAndCountAll({ where: condition, attributes: ["id", "nik"], include: [ { model: profil_penduduk, // name of reference table attributes: ['nama' , 'alamat', 'status_warga'], } ], limit, offset, }); ```
about 4 years ago · Juan Pablo Isaza 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!