English 中文(简体)
Error: Invalid Value when queing aday category field using sequelize^6.27.0 错误: 无效价值
原标题:Error: Invalid value when querying a date type field using sequelize^6.27.0 Error: Invalid value

我正致力于一个明确项目,我利用管理办公室的后遗症,使我用Mysql 8.0的图表数据得以持续。 我试图进行简单的DELETE查询,其中我对“指标_Name”和“日期”这两个领域提出疑问。

const deleteRecordsBetweenDates = async () =>{
    let startDate = new Date("2020-05-25").toJSON().slice(0, 10);
    let finalDate = new Date("2022-12-19").toJSON().slice(0, 10);
    const deleteRecords = await indcds_scheme.destroy({
        where: {
            [Op.and]: [
                {"Nombre_indicador": "Covid 19"}, 
                {[Op.between] : [
                    {Fecha_de_inicio: "2020-05-25"}, 
                    {Fecha_de_inicio: "2022-12-19"}
                ]}
            ]
        } 
    });
    return deleteRecords;
}

   router.delete("/delete", async (req, res) => {
    deleteRecordsBetweenDates()
      .then((result) => {
        console.log(result)
        return res.sendStatus(200)
      })
      .catch((error) => {
        console.log(error)
        return res.sendStatus(500)
      });
  });

计划:

const indcds_scheme = connection.define(
  "indicador_categoría",
  {
    Id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    Nombre_indicador: Sequelize.DataTypes.STRING,
    Descripción_indicador: Sequelize.DataTypes.INTEGER,
    Valor_de_variable: Sequelize.INTEGER,
    Última_actualización: Sequelize.DATE,
    Fecha_de_inicio: Sequelize.DATEONLY,
    Población_rural: Sequelize.INTEGER,
    Población_urbana: Sequelize.INTEGER,
 },
 {
   freezeTableName: true,
 }
);

接下来是:

将日期改为有效日表,但仍然存在同样的问题:

 let startDate = new Date("2020-05-25").toJSON().slice(0, 10);

“enter像

问题回答

The issue could be with the way you are using Sequelize s query options for date ranges or the JSON conversion.
I think the use of Op.and to combine conditions is unnecessary in this case. Because sequelize implicitly treats multiple keys within the where object as an AND operation anyway. So you can directly use an array of conditions without explicitly using Op.and. Also the usage of Op.between could be adjusted to include the correct date range. Instead of specifying an array of two objects inside [Op.between], you could provide an array of two dates representing the start and end dates. Something like this,

const startDate = new Date("2020-05-25")
const finalDate = new Date("2022-12-19")

const deleteRecords = await indcds_scheme.destroy({
  where: {
    Nombre_indicador: "Covid 19",
    Fecha_de_inicio: {
      [Op.between]: [startDate, finalDate],
    }
  }
})

此外,你不需要将日期改为初等干事。 如果我不错,那么,我顺便处理日期物体。 如果做不到这一点,你可以尝试使用奥特特特和奥特莱。

where: {
     Nombre_indicador: "Covid 19",
     Fecha_de_inicio: {
       [Op.gte]: startDate,
       [Op.lte]: finalDate,
     },
   }




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签