Muhammad Dyas Yaskur
Muhammad Dyas Yaskur

Reputation: 8138

Sequelize query where or is fail with empty error

I am trying to run findOne query with 'where or' but why always return on the catch with empty error without console log error?

   {
    "message": "query is fail",
    "err": {}
}

My code is below:

const Op = Sequelize.Op;

User.findOne({
    where:
            {
                [Op.or]: [
                    {
                        'username': req.body.username
                    },
                    {
                        'email': req.body.email
                    }
                     ]
            }, logging: console.log

}).then(function (user) {
    return res.status(200).send({'message': "query is success"});
}).catch(function (err) {
    return res.status(400).send({'message': 'query is fail',err});

});

I think my code is right as in the docs and also other tutorials

Upvotes: 2

Views: 2673

Answers (2)

Lucas Muñoz
Lucas Muñoz

Reputation: 21

I had a similar issue. The query was fine but there was an error after it, something like this:


User.findByPk(Id)
    .then(user =>
        console.log(id) // error because id was not defined
    )
    .catch(err =>
        return res.status(400).send({'message': 'query is fail',err});
    )

This resulted in an empty error message. The solution I found was to remove momentarily the .catch so the program would break and give a proper error message (which in my case was that "id" was not defined).

Upvotes: 2

Ved
Ved

Reputation: 746

I have used WHEREOR like this and its working, you can also try

User.findOne({
where:
        {
            $or: [
                {
                    'username': { $eq: req.body.username }
                },
                {
                    'email': { $eq: req.body.email }
                }
             ]
        }, logging: console.log

}).then(function (user) {
    return res.status(200).send({'message': "query is success"});
}).catch(function (err) {
    return res.status(400).send({'message': 'query is fail',err});

});

Upvotes: 0

Related Questions