select sequelize performs different query of parameters passed with datetime

Asked

Viewed 623 times

0

This is the function that performs the query:

router.get('/out_filtered', async (req, res, next) => {
    try {
        const dateStart = req.body.date_start;
        const dateEnd = req.body.date_end;
        console.log('start', dateStart);
        console.log('end', dateEnd);

        const where = {
            date_create: {
                [Op.between]: [dateStart, dateEnd],
            }
        };

        const result = await cashFlowOutsModel.findAll({
            where,
            logging: console.log
        });


        return res.status(200).send(result);

    } catch (err) {
        console.log(err)
        res.logger.log('error', err);
        return res.status(500).send(err);
    }
});

How I’m passing the parameters: inserir a descrição da imagem aqui How the request is done, by console log: inserir a descrição da imagem aqui Times that are passed via parameter do not match the query.

1 answer

2


When using Sequelize, it is possible that a problem occurs when recovering the time database. Sequelize converts the date time to UTC time. And as if sequelize recognizes the time in the input field as UTC time, then based on timezone, it adds 2 more hours (based on the log that Voce showed) if the timezone is of some value.

In this situation, you need to change the Sequelize setting to get the proper records without changing the time. Try this adjustment in the sequelize setting:

const sequelize = new Sequelize('db_name', 'user', 'password', {
     host: '<seu host>',
     dialectOptions: {
      useUTC: false     // para considerar a hora da consulta como a hora 
                        // local, logo nao soma +02:00 horas
     },
     dialect: 'mysql',
     timezone: '+02:00',// para salvar a data baseado no fuso horario
});

Browser other questions tagged

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