How do I count records after a specific date in Sequelize?

Asked

Viewed 201 times

-2

I am creating an API in Nodejs using Sequelize to facilitate the manipulation of data from a Mysql database. I have a list of appointments and I need to count how many appointments a patient has from today’s date.

I saw in the documentation that using [Op.gte] i can filter an age or things involving whole numbers. But I tried to use with Moment.js and the function Date and it didn’t work.

I tried the following, without success:

const pacienteConsultas = await sequelize.models.consulta.count({
  where: {
    paciente_id: paciente_id,
    data_hora: [Op.gte]: Date.now()
  }
});

How can I do?

1 answer

0


The problem is that you are passing an invalid value as an object: data_hora: [Op.gte]: Date.now()

The right thing would be data_hora: { [Op.gte]: Date.now() }

If with Date.now() not working, use new Date(). You can find a similar example with Date in documentation.


If you want to add more than one condition to the same column, the procedure is similar, placing all conditions within a single object with the column name:

/* Selecionar data_hora entre hoje e amanhã (considerando o dia e hora) */
{
  where: {
    data_hora: {
      [Op.gte]: new Date(),
      [Op.lte]: new Date(Date.now() + 24 * 60 * 60 * 1000);
    }
  }
}
  • I made the modification to data_hora: { [Op.gte]: Date.now() } but still the error continued. But I found the error. The [Op.gte] seems to be obsolete. I modified it to data_hora: { $gte: Date.now() } and it worked. Thanks for your help.

  • @Bobnunes The object Op is not obsolete, you can check on official documentation of the latest version. Maybe you are importing wrong.

  • 1

    I found this information on documentation. In v4 it still worked. But for V6 I am using, no.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.