Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var mongoose = require('mongoose');
- var UserSchema = mongoose.Schema({
- username : {
- type : String ,
- unique : true,
- required: true,
- trim : true
- },
- password : {
- type : String,
- trim : true
- }
- })
- UserSchema.pre('save', function (next) {
- var self = this;
- User.find({name : self.username}, function (err, docs) {
- if (!docs.length){
- next();
- }else{
- console.log('user exists: ',self.username);
- next(new Error("User exists!"));
- }
- });
- }) ;
- var User = mongoose.model('User' , UserSchema);
- module.exports = User;
- router.post('/login' , (req , res) => {
- var user = new User({
- username : req.body.username,
- password : req.body.password
- })
- res.send('Clear!')
- })
Add Comment
Please, Sign In to add comment