유저 데이터를 저장하고 사용하기 위한 user model, user schema 만들어야 함.
이 때, schema를 model이 감싼다 ★
1) writer : 작성한 사람
2) title: 예를 들면, ' 이 제품 좋아요' 이런 문자열. 길이는 50까지 온다.
예시)
const mongoose = require('mongoose');
const userSchema = mongoose.Schema({
name:{
type: String,
maxlength:50
},
email:{
type:String,
trim:true,
// space 없애주는 역할
unique:1
},
password:{
type:String,
minlength:5
},
lastname:{
type:String,
maxlength:50
},
role:{
// 관리자 1, 일반 유저0
type: Number
default: 0
},
image: String,
// 토큰으로 유효성 관리 가능
token:{
type:String
},
tokenExp:{
type:Number
}
})
const user = mongoose.model('user',userSchema)
// 다른 파일에서도 쓰고 싶으니까
module.exports={user}
- Trim은 이메일 쓸 때 띄어쓰기 없애주는 역할
-Token은 유효성을 관리할 수 있음 , 그래서 tokenExp도 같이 옴.
- const userSchema =mongoose.Schema({})로 열고 마지막은
const user = mongoose.model('user,userSchema)로 닫아줌.
=> schema를 model이 감싼다는 것! ★
'React' 카테고리의 다른 글
[노드]SSH 이용해서 GITHUB 연결하기 (github 소스 배포 방법) (0) | 2024.11.24 |
---|---|
[노드] GIT 설치 ( git--version 오류 해결 방법 ) (0) | 2024.11.24 |
[리액트]mongo DB 연결하기 (몽고 db 연결 에러 해결 tip) (1) | 2024.11.23 |
[리액트] NODE JS와 EXPRESS JS 다운로드 하기 (0) | 2024.11.23 |
[리액트] JSX 정의(react.createElement), 장점, 사용법(중괄호{}) (0) | 2024.11.21 |