Error :
OverwriteModelError: Cannot overwrite Dancer
model once compiled
var mongoose = require("mongoose");
const bcrypt = require('bcrypt');
const config = require( '../config/key' );
// Salt를 만들때 10자리 Salt 를 만들어서, 그 Salt를 이용해서 비밀번호를 암호화 할 것이다
const saltRounds = 10
// jsonwebtoken을 import 한다
const jwt = require('jsonwebtoken');
var mongoose = require('mongoose')
mongoose.connect( config.mongoURI , {
useNewUrlParser : true ,
useUnifiedTopology : true ,
useCreateIndex : true,
useFindAndModify : false
// 아래 코드는 연결ㄹ이 잘 됐는지 안됐는지 확인하기
}).then( () => console.log("MongoDB Connected to Dancer Collection Also... ")).catch( err => console.log( err ))
var connection = mongoose.connection;
connection.collection("dancer", function(err, collection){
if(err){
console.log("Dancer Collection connection Error");
}
var dancerSchema = new mongoose.Schema({
// user 의 name 은 무엇인지
k_name : {
type : String,
},
e_name : {
type : String,
// maxlength는 자기가 주고 싶은 만큼 주기
minlength : 1,
maxlength : 50,
trim: true,
required : true
},
email: {
type : String,
// 어떤 분이, john ahn@naver.com 이렇게 쳤고, john 다음에 들어간 빈칸을 사라지게 해준다
trim : true,
unique: 1,
required: true
},
password : {
type : String ,
minlength : 7,
required: true
},
username : {
type : String,
maxlength : 50,
required : true,
unique : 1
},
// 이렇게 role을 주는 이유는, 어떤 user 는 관리자가 될 수도 있고, 일반 user 가 될 수도 있다
role : {
// 예를 들어, number 가 1이면 일반 user 인 것이다, 0이면 dancer이다
type : Number,
},
// 아래와 같은 token을 이용해서, 유효성 같은 것들을 관리할 수 있다
token : {
type: String
},
// token의 유효기간 : token이 사용할 수 있는 기간
tokenExp : {
type: Number
},
// 비밀번호찾기위한 데이터
resetLink : {
data : String,
default : ''
},
// 전문장르
genre : {
type : String,
default: ''
} ,
// 레슨목적
Lesson_Purpose : {
type : String,
default :''
},
// 레슨 타입
Lesson_Type : {
type : String,
default :''
},
// 레슨 날
Lesson_Day : {
type : String,
default :''
},
// 레슨 시간
Lesson_Time : {
type : String,
default :''
},
// 나이
Age : {
type : String,
default :''
},
// 성별
Sex : {
type : String,
default :''
},
// Workplace
Workplace : {
type : String,
default :''
},
// Youtube Link
Youtube_Link : {
type : String,
default :''
},
// Contact
Contact : {
type : String,
default :''
}
}) // dancer Schema Definition
console.log("Dancer Collection Connected")
// 아래 User 는 위 모델의 이름을 적어준다
var Dancer = mongoose.model('Dancer', dancerSchema)
module.exports = { Dancer }
});
You have to focus upon two points
1) put new schema code "under" connect.collection("collectionname" ~~)
2) also put module.exports "under" connect.collection("collectionname" ~~)
Below is the server code that receives input from the 'profile.html'
const express = require('express');
const router = express.Router();
const path = require('path');
var { Dancer } = require('../models/Dancer');
const config = require( '../config/key' );
// profileDancer
router.get('/api/users/profileDancer', function( req , res){
res.sendFile(path.join(__dirname + "/../../client/static/templates/profileDancer.html"))
})
router.post('/api/users/profileDancer', function( req , res){
console.log(req.body);
const { genre,
Lesson_Purpose,
Lesson_Type,
Lesson_Day,
Lesson_Time,
Age,
Sex,
Workplace,
Youtube_Link,
Contact } = req.body;
// let newUser = new User( { k_name, e_name , email , password , username , role});
setTimeout(function(){
let newDancer = new Dancer( {
genre,
Lesson_Purpose,
Lesson_Type,
Lesson_Day,
Lesson_Time,
Age,
Sex,
Workplace,
Youtube_Link,
Contact });
newDancer.save( ( err, success) => {
if(err){
console.log("Error in saving Dancer Profile", err);
return res.status(400).json({ 'result' : "Error saving Dancer"});
}
console.log("Dancer Profile Input Success")
return res.status(200).json({
message : "Signup success", "success" : "true",'register_who':reigster_who
})
}) // newUser.save
}, 500)
})
// profileUser
router.get('/api/users/profileUser', function( req , res){
res.sendFile(path.join(__dirname + "/../../client/static/templates/profileUser.html"))
});
module.exports = router;
https://www.zerocho.com/category/MongoDB/post/59a1870210b942001853e250
But, The problem was not related with Schema itself,
From the very beginning,
Since, console.log("Dancer Collection connectino Error") didn't appear on the console,
Even if I defined the Dancer Schema very well, it didn't matter,,,,,
Found the "Exact" Problem
If you see the "Dancer"Schema, the username, and password is required, but, it is not included in "profile.html"
the info on username.... is put in "registration.html"
1) Dancer, User Schema defined seperately in Dancer / User.js
2) Overall Dancer.profile saving process
for example, if you see the 'genre' ,
it should have the 'list' form, since the dancer is selecting more than one genre.. but, there is only one