Skip to content
Go back

Keep in Trim

Published:  at  01:42 PM
1 min read

Reviewing a candidate take-home coding assignment solution is usually not the most thrilling task.
But when you get to learn a new trick, it gets much more interesting.

Today I learned about the trim flag for mongoose String schema type.

Say you define a new schema with a field of type String.
Adding the trim: true option will always call JavaScript .trim() method on the string value when set.

I found it especially useful for ‘email’ fields.

Example

const userSchema = new Schema({ email: { type: String, trim: true }})
const userModel = db.model('User', userSchema)

const email = '   [email protected]   '
console.log(email.length) // 23

const user = new userModel({ email: email })
console.log(user.email.length) // 17

Happy Trimming!




Previous Post
Notify Me
Next Post
howdoi