Quantcast
Channel: Node, Mongoose - $or operator not functioning as expected - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Node, Mongoose - $or operator not functioning as expected

$
0
0

Within my express app, I have a mongoose schema like so:

User model schema:

const userSchema = new mongoose.Schema({    username: {      type: String,      required: true,      unique: true,      minlength: 4,      maxlength: 50    },    email: {      type: String,      required: true,      minlength: 3,      maxlength: 255,      unique: true    },    password: {      type: String,      required: true,      minlength: 6,    },})

Here is an operation I do in one of my endpoints. I expect a user to be returned if req.body.usernameOrEmail matches a username or email of a User. Here is the operation:

let user = await User.find({ $or: [ { username: req.body.usernameOrEmail }, { email: req.body.usernameOrEmail } ] })if (!user) return res.send('No user found')

What happens is that if I put anything req.body.usernameOrEmail, the 'No user found' message is not printed as expected. Anybody know whats going wrong with the operation performed above? Thanks.


Viewing all articles
Browse latest Browse all 2

Trending Articles