繁体   English   中英

Nodejs Sequelize Attributes 获取 object 中的数据

[英]Nodejs Sequelize Attributes Get data inside an object

我有一个如下所示的 postgres 数据库:

在此处输入图像描述

我想查询这些数据并只返回其中的一部分。 下面是我写的function。

公民控制器.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); } };

这是上面 function 的响应:

在此处输入图像描述

问题是,我不想返回所有的profil_penduduk object 数据,我只想从中获取nama、alamat 和 status_warga

在此处输入图像描述

我怎样才能做到这一点? 我试着这样做:

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

但它没有用。

获取要使用的参考表详细信息的最佳方法include

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,
    });
    ```

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM