최은석

mongoDB post save test

......@@ -6,6 +6,7 @@
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.3.0",
"@testing-library/user-event": "^13.5.0",
"axios": "^0.27.2",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-scripts": "5.0.1",
......
This diff is collapsed. Click to expand it.
{
"scripts": {
"server": "cd server && nodemon server",
"client": "cd client && npm start",
"client": "cd client && npm start --port",
"start": "concurrently --kill-others-on-fail \"npm run server\" \"npm run client\""
},
"dependencies": {
......@@ -9,6 +9,7 @@
"concurrently": "^7.2.1",
"express": "^4.18.1",
"http-proxy-middleware": "^2.0.6",
"mongoose": "^6.3.4",
"nodemon": "^2.0.16"
}
}
......
const express = require('express');
const router = express.Router();
const mongoose = require('mongoose')
mongoose.connect('mongodb+srv://choieunseok:uA3mhjPcB3DwsuuD@cluster0.2gsua4u.mongodb.net/?retryWrites=true&w=majority');
const db = mongoose.connection
db.on('error', console.error)
db.once('open', () => {
console.log('Connected to mongodb Server')
});
const dayPostList = mongoose.Schema({
date: 'string',
idArray: [{ type: String }]
});
const dayPostListModel = mongoose.model('dayPostList', dayPostList);
const post = mongoose.Schema({
date: 'string',
title: 'string',
content: 'string',
password: 'string'
});
const postModel = mongoose.model('post', post);
// router.get('/api', (req, res) => {
// res.send({ test: "hi" });
// });
router.get('/api/getAll', (req, res) => {
dayPostListModel.find(function (error, dayPostLists) {
console.log('--- Read all ---');
if (error) {
res.send(error);
} else {
res.send(dayPostLists);
}
})
});
router.get('/api/testSave', async(req, res) => {
var isFirst = false;
var testDayPostList = await dayPostListModel.findOne({date: '2022-05-30'});
if(testDayPostList == null){
testDayPostList = new dayPostListModel({ date: '2022-05-30', idArray: [] });
isFirst = true;
}
var postListArr = testDayPostList.idArray;
var newPost = new postModel({ date: '2022-05-30', title: '테스트 제목', age: '테스트 내용', password: 'password' });
var newPostData = await newPost.save();
postListArr.push(newPostData._id.toString());
if(isFirst) await testDayPostList.save();
else await dayPostListModel.updateOne({date: '2022-05-30'},{idArray: postListArr});
res.send("test");
});
module.exports = router;
const express = require('express');
const router = express.Router();
router.get('/api', (req, res)=>{
res.send({ test: "hi"});
});
module.exports = router;
\ No newline at end of file
const express = require('express');
const app = express();
const test = require('.//Router/test');
const api = require('./Router/api');
app.use('/', test);
app.use('/', api);
const port=23023;
app.listen(port, ()=>{console.log(`Listening on port ${port}`)});
\ No newline at end of file
const port = 23023;
app.listen(port, () => { console.log(`Listening on port ${port}`) });
\ No newline at end of file
......