Sequelize.or返回Limit 1而不是结果

时间:2016-12-17 23:44:16

标签: javascript node.js express passport.js sequelize.js

我正在使用SequelizeJS(MySql)和Passportjs进行身份验证 当我写作

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

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

Generats

  

执行(默认):SELECT idnameusernameemailpasswordPictureroleDescriptionjoinedAtSocialcreatedAtupdatedAt FROM Users AS User LIMIT 1;

我不明白发生了什么,我正在使用或者它会生成限制1的查询!

我的用户模型

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;
}

1 个答案:

答案 0 :(得分:2)

Sequelize中没有函数Model.find(),因此看起来您可能正在调用Model.findOne()LIMIT 1应用于您的查询。要查找所有结果,您需要使用Model.findAll()

您应该使用query operator $或,但它必须位于具有OR值数组的where元素内。请参阅下面的示例。

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