簡體   English   中英

如果有日期,sequelize nodejs 按日期搜索 findAndCountAll,如果沒有日期,則按所有時間搜索

[英]sequelize nodejs search findAndCountAll by date if there is date and all time if there is not date

我想從 nodejs 請求 Sequelize 到 mysql 到來自查詢的日期數據,可能沒有任何日期

其實我不知道如何按日期搜索,但我認為它可以與參數

誰能幫我

這是我嘗試過的:

.findAndCountAll({
          where: {
                [Op.or]: [
                    { createdAt: { [Op.eq]: moment("2023-01-15").toDate() } },
                ]
})

當我將反應中的日期作為字符串發送並在 nodejs 中將其更改為日期時間時,它將顯示日期前一天

前任:

console.log(moment(2023-01-15).toDate())
// 2023-01-14T21:00:00.000Z

如果存在date ,您可以構建過濾器並使用$between過濾前一天:

const date = "2023-01-15";// Some date you received from the client or empty
let where = {};
if (date) {
    // Filter day before date
    const endDate = moment(date).toDate();
    const startDate = endDate;
    startDate.setDate(startDate.getDate() - 1);
    where = {
        from: { $between: [startDate, endDate] },
    }
}
// Execute the query
.findAndCountAll(where);

暫無
暫無

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

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