javascript - How do I validate input with MongoDB? -


i have simple little user registration form looks this:

// post register new user exports.new = function(req, res) {     var db = require('mongojs').connect('localhost/busapp', ['users']);     db.users.ensureindex({email:1}, {unique: true})      function user(email, username, password, datecreated) {         this.email = email;         this.username  = username;         this.password = password;         this.datecreated = new date();         this.admin = 0;         this.activated = 0     }      if (req.body.user.password !== req.body.user.passwordc) {         res.send('passwords not match');     } else {          var user = new user(req.body.user.email, req.body.user.username,                              req.body.user.password);          // todo: remove after clarify works.          console.log(user.email + " " + user.username + " " +                       user.password);           // save user database          db.users.save(user, function(err, saveduser) {             if (err) {                 res.send(err);             } else {             console.log("user " + saveduser.email + " saved");             }         });     } } 

but i'm having trouble validating information submitted, unique values, empty, sort of thing, nobody can send post requests database bypass jquery validation functions. i've read through docs cannot seem right. tried setting ensureindex, but, doesn't seem work. information on how validate input on database side great thanks!

one of strengths/features of mongodb flexible schema. mongodb not impose specific contraints on fields types. in general web applications, should try validation possible .. first @ client (javascript) level, application, , last resort in database server.

mongodb validation

mongodb can limited amount of validation such ensuring unique index. data validation such required fields or field types (string, integer, ..) should done in application code.

clientside/application validation

you use jquery validation, effective in client (browser view). validation should done in application code/model, otherwise disabling javascript in browser simple way insert invalid data.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -