Node.js mongoose Schema 배열

이건선·2023년 2월 28일
0

Node.js

목록 보기
15/32

문제점

postsSchema의 comments의 값을 commentsSchema의 배열로 받게하고 싶었다.

알게된 점

여러가지 말도 안되는 시도를 해봤는데 의외로 답은 간단했다.

다른 파일에 존재하는 commentsSchema를 module.exports하고 그 것을 프로퍼티의 배열로 넣어주면 해결된다.

const commentsSchema = new mongoose.Schema({...})
  
...

module.exports = commentsSchema;

const commentsSchema = require("./comment.js");

const postsSchema = new mongoose.Schema({
  
  ...
  
comments: [commentsSchema],
  
  ...

단, 이런식으로 사용 할 때 comments의 테이블은 따로 생성되는 것이 아니라 post안에 생성된다

profile
멋지게 기록하자

0개의 댓글