포스트맨에서 Success 나오는데 서버로가면 값이 안나와요ㅠ
538
작성한 질문수 1
//index.js
const express = require('express')
const app = express()
const port = 5000
const bodyParser = require('body-parser');
const config = require('./config/key');
const { User } = require("./models/User");
//application/x-www-form-urlencoded
app.use(express.urlencoded({extended: true}));
//application/json
app.use(express.json());
const mongoose = require('mongoose')
mongoose.connect(config.mongoURI,{
useNewUrlParser: true,useUnifiedTopology:true,useCreateIndex:true,useFindAndModify:false
}).then(() =>console.log('MongoDB Connected...'))
.catch(err =>console.log(err))
app.get('/', (req, res) => res.send('Hello SOSO!!!!!!!!!!!!!!!~~'))
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 saltRounds = 10;
const userSchema = mongoose.Schema({
name: {
type: String,
maxlength:50
},
email: {
type: String,
trim:true,
unique:1
},
password:{
type: String,
minlength: 5
},
lastname:{
type:String,
maxlength:50
},
role: {
type:Number,
default:0
},
image:String,
token: {
type:String
},
tokenExp: {
type:Number
}
})
userSchema.pre('save',function( next ){
var user = this;
if(user.isModified('password')){
//비밀번호 암호화시킨다.
bcrypt.genSalt(saltRounds, 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();
})
})
}
else{
next();
}
})
const User = mongoose.model('User',userSchema)
module.exports = { User }
req.body 찍어보면 빈객체로 나와요
답변 1
1
자문자답입니다.
저같은 분들 있을까봐 올려용..
POSTMAN 보내는 형식을 TEXT로 하고 삽질하고있었네요..
혹시나 다른분들도 이런 실수 안하길 바라면서 자문자답하고갑니당
깃 이메일이랑 비번이 필요하다고 하네요
0
38
1
404 에러
0
105
1
34강 인증 체크에서 element 사용 때문에 에러나시는 분들 이렇게 하심 됩니다.
0
121
1
로그인, 로그아웃, 토근 작동 안 함
0
242
0
9강 오류 어떻게 해결하나요?
0
195
1
localhost 에서 연결을 거부했습니다.
0
1944
4
포스트맨에서 true가 안떠요
0
152
1
왜 안되나요
0
130
1
몽고db 연결 오류가 납니다 위에껀 입력한 코드, 아래껀 터미널이에요
0
244
1
로그아웃 401 에러(Unauthorized)
0
506
2
암호가 해싱되지 않고 입력값 그대로 db에 저장되는 문제
0
152
1
7강중에서
0
167
2
User.findByToken is not a function
0
214
1
루트 디렉토리
0
275
1
useState
0
564
1
프록시 잘 설정했는데도 404 오류 뜨는 분들
5
879
6
webpack 관련 에러 질문
0
221
1
리액트 관련 질문
0
275
1
14강 로그아웃 안됨
0
319
1
mongoDB 데이터 확인하는 법
0
413
1
postman 에러
0
295
1
선생님 리덕스를 사용하면 어떠한 부분이 좋은지 알 수 있을까요?
0
236
1
다음과 같은 에러들이 발생합니다.
0
276
1
14강 로그아웃 기능 구현시 postman에서 Cannot POST 오류가 뜹니다.
0
380
1





