-
카테고리
-
세부 분야
풀스택
-
해결 여부
미해결
몽고DB에 데이터집어넣을때..ㅠㅠㅠ
20.05.07 16:00 작성 조회수 122
0
아래그림과 같이 집어넣어지는데.. ㅠㅠㅠ 정확히 다 따라쳤다고 생각했는데 어떤것이 잘못됐을까요 ㅠ
이건 user.js 코드입니다 ㅠ
// user모델과 스키마를 정의
const bcrypt = require('bcrypt');
const saltRounds = 10;
const mongoose = require('mongoose');
const jwt =require("jsonwebtoken")
const userSchema = mongoose.Schema({
name: {
type: String,
maxlength: 50
},
email: {
type: String,
trim: true,
},
password: {
type: String,
minlength: 5
},
role: {
type: Number,
default: 0
},
image: String,
token: {
type: String
},
tokenExp:{
type: Number
}
})
userSchema.pre('save',function (next){
var user = this; // 위에 모델들을 가리킨다.
//비밀번호를 암호화시킨다.
//but 비밀번호가 변화될때만!!
if(user.isModified('password')){
bcrypt.genSalt(saltRounds, function(err,salt){
bcrypt.hash(myPlaintextPassword,salt,function(err,hash){
if(err) return next(err)
//salt를 잘 생성했다면
bcrypt.hash(user.password , salt,function(err,hash){
if(err) return next(err)
user.password = hash
next() // index.js에 save 함수로 돌아간다.
} )
})
}) }
else {
next()
}
}) //유저모델에 유저 정보를 저장하기 전에 무엇을 한다 이게 다끝나면
userSchema.methods.comparePassword = function(plainPassword, cb){
// plainpassword 1234567
bcrypt.compare(plainPassword, this.password, function(err,isMatch){
if(err) return cb(err),
cb(null,isMatch)
})
}
userSchema.methods.generateToken = function(cb) {
// jsonwebtoken을 이용해서 토큰을 생성하기
var user = this;
var token = jwt.sign(user._id.toHexString(), 'secretToken')
// user._id+'secretToken' = token //이 token을 가지고 이사람이 누군지
user.token = token
user.save(function(err,user){
if(err) return cb(err)
cb(null,user)
})
}
const User = mongoose.model('User',userSchema);
module.exports = {User};
답변을 작성해보세요.
1
John Ahn
지식공유자2020.05.07
혹시 깃헙에 올리신 코드를 주실수 있을까요 ~ ? ㅎ 제가 직접 해봐서 찾는게 더 빠를것같아서요 ~ !
답변은 새로운 글로 올려주시면 감사하겠습니다 ^^
0
0
0
답변 4