강의

멘토링

커뮤니티

인프런 커뮤니티 질문&답변

성동원님의 프로필 이미지
성동원

작성한 질문수

따라하며 배우는 노드, 리액트 시리즈 - 기본 강의

register 에러입니다.

작성

·

236

0

안녕하세요. 강의 잘 보고 있습니다.

postman send 시 계속 false가 나오는데, err 메세지를 구체적으로 보려면 어떻게 디버깅해야하는 건가요?

index.js 파일 첨부합니다.

const express = require('express')
const app = express()
const port = 5000
const bodyParser = require('body-parser');
const {User} = require("./models/User");
const config = require("./config/key");

//application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({extended:true}));
//application/json 타입을 분석해서 가져올 수 있게 해준 거
app.use(bodyParser.json());

const mongoose = require('mongoose');

mongoose.connect(config.mongoURI,{
  useNewUrlParser: trueuseUnifiedTopology:trueuseCreateIndex:trueuseFindAndModify:false
}).then(()=> console.log('MongoDB Connected'))
  .catch(err => console.log(err))

app.get('/', (reqres=> {
  res.send('Hello World!')
})

//register 라우터
//body-parser을 통해서 클라이언트 > 서버 로 넘어오는 데이터를 받을 수 있다. npm install body-parser --save
app.post('/register',(req,res)=> {
  //회원 가입할때 필요한 정보들을 client에서 가져오면 그것들을 데이터베이스에 넣어준다.

  const user = new User(req.body)
  user.save((err,userInfo)=>{    
    if(err)return res.json({success:false,err})
    return res.status(200).json({
      success:true
    })
  })  
  
})


app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

user.js 파일 첨부합니다.

const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const { logger } = require('..');
const saltRounds = 10;

const userSchema = mongoose.Schema({
    name : {
        type : String,
        maxlength:50    
    },
    email:{
        type:String,
        trim:true,
        unique:1
    },
    password:{
        type:String,
        minlength:5
    },
    role:{
        type:Number,
        default:0
    },
    image:String,
    token:{
        type:String        
    },
    tokenExp:{
        type:Number
    }

})

userSchema.pre('save',function(next){
    var use = this;

    if(user.isModified('password')){
        //비밀번호를 암호화 시킨다.
        bcrypt.genSalt(saltRounds,function (err,salt) {
            if(errreturn next(err)
            logger.info('ddddd');
            bcrypt.hash(user.password,salt,function(err,hash){
                if(errreturn next(err)
                user.password = hash
                next()
            })
        })
    } else {
        next()
    }
})

//스키마를 모델로 감싼다
const User = mongoose.model('User',userSchema)
//다른파일에서도 이 모델을 사용할 수 있도록

module.exports = {User}

서버 켜져있습니다.

index.js에 console.log(req.body)했을 시 postman 입력값 전송됨을 확인하였습니다. 

포스트맨 결과입니다.

조언 부탁드립니다.

답변 2

0

성동원님의 프로필 이미지
성동원
질문자

ㅠㅠ 켁... User.js에서 var user = this;가 아니라 var use = this;를 해서 발생한 에러네요. ㅠㅠ 해결했습니다 ^^ 

0

John Ahn님의 프로필 이미지
John Ahn
지식공유자

우선 코드를 보면  에러 메시지가 잘 나와야 될 것 같은데 안나오니깐 특이하네요 ....
혹시 

register 하는 코드에서 

console.log("err", err) 이런식으로 해서 

아무것도 안나오는지 봐주시겠나요 ~ ?

성동원님의 프로필 이미지
성동원

작성한 질문수

질문하기