A
A
Alex102142021-08-25 18:04:36
ORM
Alex10214, 2021-08-25 18:04:36

How to filter an array in sequelize?

In the database I have a table with columns "start_date", "end_date" and other columns. But I need "start_date", "end_date" data.
This is how I query data from a table:

const getStartEndDateInTradePage = require('../models/Trades');

module.exports.getStartEndDateInTradePage = async (req, res) => {
  try {
    const arr = await getStartEndDateInTradePage.findAll(
      {
        attributes: ['open_date', 'close_date']
      }
    )
    res.status(201).json(arr);
  } catch (e) {
    console.log(e)
    res.status(500).json({
      message: 'Server ERROR!'
    })
  }
}

The data comes in and everything would be fine, BUT here is an example of what data I receive:
[
  {open_date: "2002-07-30", end_date: "-"},
  {open_date: "2002-07-30", end_date: "2002-09-06"},
  {open_date: "2002-07-30", end_date: "-"},
  {open_date: "2002-07-30", end_date: "2002-09-06"},
  {open_date: "2002-09-06", end_date: "2002-12-06"},
  {open_date: "2002-09-06", end_date: "-"},
  {open_date: "2002-09-06", end_date: "2002-12-06"},
]

Tell me how you can filter in sequelize in such a way that if there is a dash in "end_date", then the object is not added to the array. It is important that filtered data comes to the front. Since there are a lot of them.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Slava Rozhnev, 2021-08-25
@Alex10214

RTFM

const arr = await getStartEndDateInTradePage.findAll(
      {
        attributes: ['open_date', 'close_date'],
        where: {
            close_date: {
              [Op.ne]: '-'
            }
          }
      }
    )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question