node.js - NodeJS Mongoose Schema 'save' function error handling? -


i having problem outputting error user using res.send(err), being called in callback of mongoose user schemas 'save' function. note when use console.log(err), show's expected error (such username short), res.send outputting '{}' in postman when sending request post values should cause error.

also wondering if should doing input validation in router or in mongoose user schemas .pre function? putting validation there seems correct, keeps node router file cleaner.

here code in question...

app/routes/apirouter.js

var user = require('../models/user'); var bodyparser = require('body-parser'); ... apirouter.post('/users/register', function(req, res, next) {       var user = new user;     user.name = req.body.name;     user.username = req.body.username;     user.password = req.body.password;      user.save(function(err) {         if (err) {             console.log(err);             res.send(err);         } else {             //user saved!             res.json({ message: 'user created' });         }     });  }); ... 

app/models/user.js

var mongoose = require('mongoose'); var schema = mongoose.schema; var bcrypt = require('bcrypt-nodejs'); var validator = require('validator');  var userschema = new schema({     name: string,     username: { type: string, required: true, index: {unique: true} },     password: { type: string, required: true, select: false } });  userschema.pre('save', function(next) {     var user = this;      if (!validator.islength(user.name, 1, 50)) {         return next(new error('name must between 1 , 50 characters.'));     }      if (!validator.islength(user.username, 4, 16)) {         return next(new error('username must between 4 , 16 characters.'));     }      if (!validator.islength(user.password, 8, 16)) {         return next(new error('password must between 8 , 16 characters.'));     }      bcrypt.hash(user.password, false, false, function(err, hash) {         user.password = hash;         next();     }); });  userschema.methods.comparepassword = function(password) {     var user = this;     return bcrypt.comparesync(password, user.password); };  module.exports = mongoose.model('user', userschema); 

from quick glance, looks using express. when object or array passed res.send() (like in case error occurs), defaults using json.stringify on object/array , sets content-type application/json. (ref: http://expressjs.com/4x/api.html#res.send). message property of error object not serialized when passed through json.stringify because defined enumerable being false.

ex.

 $ node  > var err = new error('this test')  undefined  > console.log(json.stringify(err))  {}  undefined 

is not possible stringify error using json.stringify? has examples of how make sure message property (and others if like) included.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -