簡體   English   中英

使用 Where 和 Include 失敗的 Sequelize 查詢

[英]Sequelize query with Where and Include failing

試圖查詢以返回汽車和相關照片。 這有效:

Vehicle.findAll({
        include: [db.photos]
    })

我得到一個 output 像:

{
    "id": 69,
    "make": "Ford",
    "model": "Fiesta",
    "photos": [
        {
            "id": 10,
            "photoUrl": "29_1.jpg",
            "vehicleId": 69
        },
        {
            "id": 11,
            "photoUrl": "29_2.jpg",
            "vehicleId": 69
        }
    ]
},

但事實並非如此,只返回沒有照片的汽車數據:

Vehicle.findAll({
        where: { 
            make: { [Op.like]: '%' + make + '%' },
            model: { [Op.like]: '%' + model + '%' }
        }
    },
    {
        include: [db.photos]
    })

我究竟做錯了什么?

您沒有正確使用Model.findAll() Model.findAll()的簽名是

公共 static async findAll(options: object): Promise<Array>

如您所見,它只接受一個參數options

嘗試:

Vehicle.findAll({
  where: {
    make: { [Op.like]: '%' + make + '%' },
    model: { [Op.like]: '%' + model + '%' },
  },
  include: [db.photos],
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM