Commit 3016b0cf authored by KANTAPONG SONG-NGAM's avatar KANTAPONG SONG-NGAM

add pro

parent 594a3bc4
Schema = require('mongoose').Schema
ThingSchema = new Schema({
Name : {
type : String,
required : true
},
Data : {
type : [{
Value : {
type : Number,
min : 0
},
Time : {
type : Date
}
}]
},
userId : {
type : Schema.Types.ObjectId
}
})
module.exports = mongoose.model('Things', ThingSchema)
Schema = require('mongoose').Schema
bcrypt = require('bcrypt')
SALT_WORK_FACTOR = 10
UserSchema = new Schema({
username : {
type : String,
required : true
},
password : {
type : String,
required : true
}
})
UserSchema.pre('save', function(next) {
user = this
if (!user.isModified('password'))
return next()
bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) {
if (err) return next (err)
bcrypt.hash(user.password, salt, function(err, hash) {
if (err) return next (err)
user.password = hash
next()
})
})
})
UserSchema.methods.comparePassword = function(candidate, cb) {
bcrypt.compare(candidate, this.password, function(err, isMatch) {
if (err) return cb (err)
cb(null, isMatch)
})
}
module.exports = mongoose.model('Users', UserSchema)
mongoose = require('mongoose')
Things = require('./Things')
Users = require('./Users')
mongoose.connect('mongodb://localhost:27017/Students',{useNewUrlParser:true});
add_User = new Users({
username: 'test',
password: 'pass'
})
add_User.save(function(err) {
if (err) throw err
console.log('save USer completed.')
})
{
"name": "project",
"version": "1.0.0",
"lockfileVersion": 1
}
{
"name": "project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment