Seeking help to hide the password object from being displayed. Below is my code snippet where I am using bcrypt to hash the password. Even though I am trying to hide the return object, I am not seeing the desired outcome. Can someone please point out what I might be doing wrong? Any assistance would be greatly appreciated. Thank you.
var express = require('express')
var router = express.Router()
var User = require('../Models/User.js')
var bcrypt = require('bcrypt')
router.get('/:resource', function(req, res, next){
var resource = req.params.resource
if (resource == 'user'){
User.find(null, function(err, users){
if(err) {
res.json({
confimration: 'error',
message: err
})
return
}
res.json({
confimration: 'success',
message: users
})
return
})
}
})
router.post('/:resource', function(req, res, next){
var resource = req.params.resource
var data = req.body
var password = data.password
var hashed = bcrypt.hashSync(password, 10)
data['password'] = hashed
if(resource == "user") {
User.create(data, function(err, user){
if(err){
res.json({
confirmation: 'fail',
message: err
})
return
}
res.json({
confirmation: 'success',
result: user
})
return
})
}
})
module.exports = router
var mongoose = require('mongoose')
var UserSchema = new mongoose.Schema({
firstName: {type: String, lowercase: true, trim: true, default: ''},
lastName: {type: String, lowercase: true, trim: true, default: ''},
email: {type: String, lowercase: true, trim: true, default: ''},
city: {type: String, default: ''},
password: {type: String, default: ''},
timestamp: {type:Date, default: Date.now}
})
UserSchema.methods.summary = function() {
var summary = {
firstName: this.firstName,
lastName: this.lastName,
email: this.email,
timestamp: this.timestamp,
id: this._id,
city: this.city
}
return summary
}
module.exports = mongoose.model('UserSchema', UserSchema)
{
_id: "57f460235805b52762605df2",
__v: 0,
timestamp: "2016-10-05T02:06:27.829Z",
password: "$2a$10$DIHrMO8WcRmOkIVj93SSQ.LFe5vPYH6R3xrfsSuql.v2jfU2mcO.C",
city: "new york",
email: "4",
lastName: "4",
firstName: "4"
}