NodeJS 如何使用Sequelize在API中以相同的顺序放置属性?

x759pob2  于 2022-12-03  发布在  Node.js
关注(0)|答案(1)|浏览(148)

我试图通过使用Sequelize将API中的属性按相同的顺序排列。我该如何做到这一点?下面是我的控制器函数和GET/API结果:

  • -------控制器功能---------
bookList (req,res){
        const {page , size } = req.query
        return Book
        .findAll({
          attributes:['id',"book"],
          include: [{
            model: Author,
            as: 'author',
            attributes:['author']
          },{
            model: Category,
            as: 'category',
            attributes:['category']
          },{
            model: Publisher,
            as: 'publisher',
            attributes:['publisher']
          }],
           limit: size,
           offset: size * page
        })

        .then((result) => res.status(200).send(result))
        .catch((error) => res.status(400).send(error));
      }
  • --------美国石油学会结果-------------
[
    {
        "id": 6,
        "book": "Şehir-Kültür-İstanbul",
        "author": {
            "author": "İlber Ortaylı"
        },
        "category": {
            "category": "Hukuk"
        },
        "publisher": {
            "publisher": "Doğan Yayınları"
        }
    },
    {
        "id": 7,
        "book": "Kürk Mantolu Madonna",
        "author": {
            "author": "Sabahattin Ali"
        },
        "category": {
            "category": "Psikoloji"
        },
        "publisher": {
            "publisher": "Timaş Yayınları"
        }
    },
    {
        "id": 8,
        "book": "Mahur Beste",
        "author": {
            "author": "Sabahattin Ali"
        },
        "category": {
            "category": "Edebiyat"
        },
        "publisher": {
            "publisher": "Timaş Yayınları"
        }
    },
    {
        "id": 9,
        "book": "Aydaki Kadın",
        "author": {
            "author": "Sabahattin Ali"
        },
        "category": {
            "category": "Psikoloji"
        },
        "publisher": {
            "publisher": "Timaş Yayınları"
        }
    },
    {
        "id": 10,
        "book": "Başlangıç",
        "author": {
            "author": "Dan Brown"
        },
        "category": {
            "category": "Edebiyat"
        },
        "publisher": {
            "publisher": "Altın Kitaplar"
        }
    }
]

我想在API中以相同的顺序放置属性。如下所示:

{
        "id": 6,
        "book": "Şehir-Kültür-İstanbul",
        "author": "İlber Ortaylı" ,
        "category": "Hukuk",
        "publisher":"Doğan Yayınları"
        }
ht4b089n

ht4b089n1#

我想这个应该够了

bookList (req,res){
        const {page , size } = req.query
        return Book
        .findAll({
          attributes: [
            ['id', 'book', 'author', 'category', 'publisher'],
            [sequelize.col('author.author'), 'author'],
            [sequelize.col('category.category'), 'category'],
            [sequelize.col('publisher.publisher'), 'publisher']
          ],
          include: [{
            model: Author,
            as: 'author',
            attributes:['author']
          },{
            model: Category,
            as: 'category',
            attributes:['category']
          },{
            model: Publisher,
            as: 'publisher',
            attributes:['publisher']
          }],
           limit: size,
           offset: size * page
        })

        .then((result) => res.status(200).send(result))
        .catch((error) => res.status(400).send(error));
      }

相关问题