-
글쓴이글
-
2023년 5월 8일 16:23 #81681
Choi BI참가자하루 종일 리서치, 수정해봤는데도 답이 안나와 질문 드립니다ㅠ 아래처럼 코드를 작성해서 http://localhost:8000/list 페이지로 이동할 경우, 무한 로딩 현상이 일어나면서 페이지 이동이 안되는데 이유가 뭘까요..?
const express = require("express"); const app = express(); const bodyParser = require("body-parser"); app.use(bodyParser.urlencoded({ extended: true })); const MongoClient = require("mongodb").MongoClient; app.set("view engine", "ejs");
app.listen(8000, function () { console.log("연결완료"); });
var db; MongoClient.connect( "mongodb+srv://qldnjs23:<비밀번호>@cluster0.am3ri5e.mongodb.net/?retryWrites=true&w=majority" ) .then(function (client) { db = client.db("todoapp"); // db 변수에 DB 연결 객체 할당 console.log("DB 연결 완료"); }) .catch(function (err) { console.log(err); });
app.get("/list", function (req, res) { db.collection("post") .find() .toArray(function (error, result) { console.log(result); res.writeHead(200, {"Content-Type": 'text/html;charset=UTF-8'}); res.render("list.ejs", { posts: result }); }); });
app.get("/write", function (req, res) { res.sendFile(__dirname + "/write.html"); });
app.post("/add", function (req, res) { res.send("전송완료"); db.collection("post").insertOne( { title: req.body.title, pw: req.body.pw }, function (에러, 결과) { console.log(req.body.title); console.log(req.body.pw); console.log("저장완료"); } ); });
app.get("/beauty", function (req, res) { res.send("beauty page"); });
감사합니다.
2023년 5월 8일 16:30 #81688
Choi BI참가자app.get("/list", function (req, res) 부분에 아래와 같은 오류가 뜨긴 합니다..
TypeError: Cannot read properties of undefined (reading 'collection')
2023년 7월 6일 00:35 #89797
손유정참가자같은 증상입니다. app.get('list'~~~) 코드가 이미 밖에 나와있는데 무한로딩되며 실행이 안됩니다. db에 데이터 4개 정상 저장되어있고, write에서 추가 잘됩니다.
const express = require('express') const app = express() const bodyParser = require('body-parser'); app.use(express.urlencoded({extended : true}));
const mongodb = require('mongodb') const MongoClient = mongodb.MongoClient; const url = 'mongodb+srv://admin:qwer1234@cluster1.eiwfukf.mongodb.net/?retryWrites=true&w=majority' var db;
//ejs 쓸거임 추가 app.set('view engine', 'ejs');
//----------------------------------------------------// MongoClient.connect(url) .then(client => { db=client.db('todoapp'); console.log('connected'); // console.log(client); }) .then(app.listen(8080, () => { console.log('listening on 8080'); })); /**index.html 실행 코드 */ app.get('/', function(req,res) { res.sendFile(__dirname + '/index.html') });
/** 누군가가 /pet으로 방문을 하면 pet 관련한 안내문을 띄워주자 */ app.get('/pet', function(req, res){ res.send('펫이다') });
app.get('/beauty', function(req,res){ res.send('뷰티다') }) ;
app.get('/write', function(req,res){ res.sendFile(__dirname + '/write.html') });
app.post('/add', function(req,res){ db.collection('post').insertOne({title:req.body.title, date:req.body.date}, function(err, result){
}) res.send('전송시마이'); console.log(req.body.title) console.log(req.body.date) });
/** * /list 로 get 요청으로 접속하면 * 실제 DB에 저장된 데이터들로 꾸며진 html을 보여줌 * */ app.get('/lists', function(req, res){ /**db에 저장된 post 라는 collection 안의 모든 데이터를 꺼내주세요 */ db.collection('post').find().toArray(function(err, result){ console.log(result); // res.render('list.ejs', {posts : result}); }); });
2023년 7월 6일 22:24 #89930
손유정참가자넵 주석 없이 했을 때 안되어서 주석처리 해보았던 것인데 다시 주석지우고 해도 안됩니다 ㅜ 무한로딩입니다 (최소10분 이상) 오류의 대부분은 오타이지만 6개월 전에 정상작동했던 코드 복붙해도 안되니 오타가 아니라 다른 원인인 듯 합니다...
2023년 7월 7일 10:17 #89964
codingapple키 마스터복붙해보니까 /lists 접속 잘되는데 npm install ejs 했는지 views폴더에 list.ejs있나 확인해봅시다
-
글쓴이글
- 답변은 로그인 후 가능합니다.