몽고DB에 데이터집어넣을때..ㅠㅠㅠ
310
동관
작성한 질문수 8
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};
답변 4
깃 이메일이랑 비번이 필요하다고 하네요
0
35
1
404 에러
0
102
1
34강 인증 체크에서 element 사용 때문에 에러나시는 분들 이렇게 하심 됩니다.
0
119
1
로그인, 로그아웃, 토근 작동 안 함
0
239
0
9강 오류 어떻게 해결하나요?
0
194
1
localhost 에서 연결을 거부했습니다.
0
1927
4
포스트맨에서 true가 안떠요
0
150
1
왜 안되나요
0
129
1
몽고db 연결 오류가 납니다 위에껀 입력한 코드, 아래껀 터미널이에요
0
243
1
로그아웃 401 에러(Unauthorized)
0
504
2
암호가 해싱되지 않고 입력값 그대로 db에 저장되는 문제
0
149
1
7강중에서
0
162
2
User.findByToken is not a function
0
211
1
루트 디렉토리
0
271
1
useState
0
560
1
프록시 잘 설정했는데도 404 오류 뜨는 분들
5
875
6
webpack 관련 에러 질문
0
219
1
리액트 관련 질문
0
272
1
14강 로그아웃 안됨
0
318
1
mongoDB 데이터 확인하는 법
0
410
1
postman 에러
0
291
1
선생님 리덕스를 사용하면 어떠한 부분이 좋은지 알 수 있을까요?
0
234
1
다음과 같은 에러들이 발생합니다.
0
273
1
14강 로그아웃 기능 구현시 postman에서 Cannot POST 오류가 뜹니다.
0
379
1





