简体   繁体   中英

Sequelize.or returns Limit 1 rather than or result

i'm using SequelizeJS(MySql) with Passportjs for authentication when i write

User.find(db.Sequelize.or({ 'username': username }, { 'email': req.body.email }) )
            .then((user) => {console.log(user)}

or

User.find({$or:[({ 'username': username }, { 'email': req.body.email })]} )

Generats

Executing (default): SELECT id , name , username , email , password , Picture , role , Description , joinedAt , Social , createdAt , updatedAt FROM Users AS User LIMIT 1;

I don't understand what happen , i'm using or and it generates query with limit 1 !

my user Model

const bcrypt = require('bcrypt-node');
const db = require('../Config/db');

module.exports = function () {
    let DataType = db.Sequelize;
    let User = db.sequelize.define('User', {
        name: { type: DataType.STRING, allowNull: false },
        username: { type: DataType.STRING, unique: true, allowNull: false },
        email: { type: DataType.STRING, unique: true, allowNull: false, validate: { isEmail: true } },
        password: {
            type: DataType.STRING, allowNull: false, set: function (pass) {
                let newPassword = bcrypt.hashSync(pass);
                this.setDataValue('password', newPassword);
            }
        },
        Picture: { type: DataType.STRING, default: '#' },
        role: { type: DataType.STRING, default: 'user',allowNull: false },
        Description: { type: DataType.TEXT },
        joinedAt: { type: DataType.DATE, defaultValue: DataType.NOW },
        Social: { type: DataType.TEXT }
    }, {
            classMethods: {
                associate: function (models) {
                    User.hasMany(models.Post);
                }
            }
        }, {
            instanceMethods: {
                comparePassword: function (password) {
                    return bcrypt.compareSync(password, this.password);
                }
            }
        });

    return User;
}

There is no function Model.find() in Sequelize, so it looks like you are probably calling Model.findOne() which applies the LIMIT 1 to your query. To find all of the results, you need to use Model.findAll() .

You should use the query operator $or, but it needs to be inside a where element with an array of OR values. See an example below.

User.findAll({
  where: {
    $or: [
      username: username,
      email: req.body.email,
    ],
  }
})
.then(users => console.log(users));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM