续集findall()

时间:2019-03-25 09:42:02

标签: node.js orm sequelize.js

我在返回数据时遇到问题。如何以目录和照片阵列的形式返回数据?我有一个请求

const catalogs = await Catalogs.findAll({
            raw: true,
            attributes: ["id", "attractions", "id_kindergarten"],
            include: {
                model: Catalogs_img,
                as: "catalogs_img"
            }
        }).then((data) => {
            console.log(data);
        });

结果如下

 [ { id: 18,
    attractions: '26,22,18',
    id_kindergarten: 104,
    'catalogs_img.id': 2,
    'catalogs_img.id_catalog': 18,
    'catalogs_img.cat': null,
    'catalogs_img.queue': 0,
    'catalogs_img.original':
     '/uploads/104/catalog/1553256869918_Hydrangeas_-_Kopia_2.jpg',
    'catalogs_img.thumbnail':
     '/uploads/104/catalog/thumb_1553256869918_Hydrangeas_-_Kopia_2.jpg',
    'catalogs_img.main_img': null,
    'catalogs_img.id_kindergarten': 104,
    'catalogs_img.createdAt': 2019-03-22T12:14:30.000Z,
    'catalogs_img.updatedAt': 2019-03-22T12:14:30.000Z },
  { id: 18,
    attractions: '26,22,18',
    id_kindergarten: 104,
    'catalogs_img.id': 4,
    'catalogs_img.id_catalog': 18,
    'catalogs_img.cat': null,
    'catalogs_img.queue': 2,
    'catalogs_img.original': '/uploads/104/catalog/1553256884832_Penguins.jpg',
    'catalogs_img.thumbnail': '/uploads/104/catalog/thumb_1553256884832_Penguins.jpg',
    'catalogs_img.main_img': null,
    'catalogs_img.id_kindergarten': 104,
    'catalogs_img.createdAt': 2019-03-22T12:14:45.000Z,
    'catalogs_img.updatedAt': 2019-03-22T12:14:45.000Z },
  { id: 18,
    attractions: '26,22,18',
    id_kindergarten: 104,
    'catalogs_img.id': 6,
    'catalogs_img.id_catalog': 18,
    'catalogs_img.cat': null,
    'catalogs_img.queue': 3,
    'catalogs_img.original': '/uploads/104/catalog/1553257392446_Hydrangeas_-_Kopia.jpg',
    'catalogs_img.thumbnail':
     '/uploads/104/catalog/thumb_1553257392446_Hydrangeas_-_Kopia.jpg',
    'catalogs_img.main_img': null,
    'catalogs_img.id_kindergarten': 104,
    'catalogs_img.createdAt': 2019-03-22T12:23:13.000Z,
    'catalogs_img.updatedAt': 2019-03-22T12:23:13.000Z } ]

如果删除raw: true,则必须使用data[0].dataValues来引用数据,并且它会正确返回数据,但格式错误。我需要目录和包含该目录照片的阵列

{ id: 18,
  attractions: '26,22,18',
  id_kindergarten: 104,
  catalogs_img:
   [ Catalogs_img {
       dataValues: [Object],
       _previousDataValues: [Object],
       _changed: {},
       _modelOptions: [Object],
       _options: [Object],
       __eagerlyLoadedAssociations: [],
       isNewRecord: false },
     Catalogs_img {
       dataValues: [Object],
       _previousDataValues: [Object],
       _changed: {},
       _modelOptions: [Object],
       _options: [Object],
       __eagerlyLoadedAssociations: [],
       isNewRecord: false },
     Catalogs_img {
       dataValues: [Object],
       _previousDataValues: [Object],
       _changed: {},
       _modelOptions: [Object],
       _options: [Object],
       __eagerlyLoadedAssociations: [],
       isNewRecord: false } ] }

这是我的关联

Catalogs.associate = function(models) {
        Catalogs.hasMany(models.Catalogs_img, {
            as: "catalogs_img",
            foreignKey: "id_catalog",
            targetKey: "id"
        });
    };

我需要这样的格式:

[
  attractions: '26,22,18',
  id_kindergarten: 104,
  catalogs_img:[
    {id:1,src:'test'},
    {id:2,src:'test'}
  ]
]

0 个答案:

没有答案