728x90
์ ๋ง...์ค๋๋ง์ ๋ชฝ๊ณ ๋ฅผ ๋ง์ง๋ ค๋ ์๋ฌ๊ฐ ์ด๋ง์ ๋ง์ด ์๋๋ฐ
๊ทธ์ค ๊ฐ์ฅ ํ๋๊ฒ ํ๋ is not a function
๋ชฝ๊ณ ๋ฒ์ ์ ๊ทธ๋ ์ด๋๋ก ์ด๋ฐ ์ํฉ์ด ๋ฐ์ํ๊ฒ์ผ๋ก ๋ณด์
์ ์๋๋ฉด ๋ค์ด๊ทธ๋ ์ด๋...ํ๊ธฐ ์ง์ ์ ํด๊ฒฐํจ
์ค๋ฅ ์์ ๋ฐฉ๋ฒ
module export ๋ฐฉ์์ด๋ ์ปจํธ๋กค๋ฌ์์ ๋ชจ๋ ๊ฐ์ ธ์ ์ฐ๋ ๋ฐฉ์ ์์
์์ ์
๋ชจ๋์ ์ธ
//์์ ์
const mongoose = require("mongoose");
//schema create
const IdeaSchema = new mongoose.Schema({
title: {
type: String,
required: true,
trim: true
},
body: {
type: String,
required: true,
trim: true
},
created: {
type: Date,
default: Date.now
}
});
module.exports = mongoose.model("idea", IdeaSchema);
idea.ctrl.js - ์ปจํธ๋กค๋ฌ
//์์ ์
const IdeaModel = require("../../models/idea");
const mongoose = require("mongoose");
const create = (req, res) => {
const { title, body, deadline } = req.body;
if (!title || !idea || !deadline) return res.status(400).end();
//์ค๋ฅ๋๋ ์์น
IdeaModel.create({ title, body, deadline }, (err, result) => {
if (err) {
res.status(500).end();
console.log(err);
} else {
res.status(201).json(result);
}
});
};
module.exports = { create }
์์ ํ
๋ชจ๋์ ์ธ
//์์ ์
const mongoose = require("mongoose");
//schema create
const IdeaSchema = new mongoose.Schema({
title: {
type: String,
required: true,
trim: true
},
body: {
type: String,
required: true,
trim: true
},
created: {
type: Date,
default: Date.now
}
});
//์ฌ๊ธฐ ์์
module.exports = {
ideaCollection: mongoose.model("idea", IdeaSchema),
};
์ปจํธ๋กค๋ฌ
//์์ ์
const IdeaModel = require("../../models/idea");
const mongoose = require("mongoose");
const create = (req, res) => {
const { title, body, deadline } = req.body;
if (!title || !idea || !deadline) return res.status(400).end();
//์ค๋ฅ๋๋ ์์น - ์ฌ๊ธฐ ์์
IdeaModel.ideaCollection.create({ title, body, deadline }, (err, result) => {
if (err) {
res.status(500).end();
console.log(err);
} else {
res.status(201).json(result);
}
});
};
module.exports = { create }
๊ด๋ จ https://stackoverflow.com/questions/41455868/mongodb-find-is-not-a-function
728x90