• 카테고리

    질문 & 답변
  • 세부 분야

    풀스택

  • 해결 여부

    미해결

7강 : postman

22.09.20 01:01 작성 조회수 347

0

index.js

const bodyParser = require('body-parser');
const {User} = require("./models/User");

//application/x-www-form-urlencoded 정보 분석
app.use(bodyParser.urlencoded({extended: true}))
//application/json파일을 분석
app.use(bodyParser.json())
app.get('/',(req, res) => {res.send('Hello Word!')})

app.post('/register',(req,res)=>{   
    //회원가입할 때 필요한 정보들을 client에서 가져오면,
    //그 정보들을 DB에 넣어준다.
    const user = new User(req.body);
    //user모델에 정보가 저장됨
    //실패 시, 실패한 정보를 보내줌
    user.save((err, userInfo) => {
        if(err) return res.json({success: false, err})
        return res.status(200).json({
            success: true
        })
    }) 
})

 

User.js

const mongoose = require('mongoose');

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
    }
})

const User = mongoose.model('User', UserSchema);
module.exports = {User}

 

TypeError: User is not a constructor

이런 에러가 나옵니다ㅠ.. mongoDB는 연결잘되었구요.

코드는 다 동일하게 작성했는데 왜이럴까요..

답변 1

답변을 작성해보세요.

0

jysrho12님의 프로필

jysrho12

질문자

2022.09.20

앗 다시 하니 되네요!