• 로그인
  • 장바구니에 상품이 없습니다.

home2 게시판 Node.js, Express 게시판 find().toArray 코드위치에 따른 결과차이 대해 질문이 있습니다

find().toArray 코드위치에 따른 결과차이 대해 질문이 있습니다

5 글 보임 - 1 에서 5 까지 (총 5 중에서)
  • 글쓴이
  • #90142

    차승환
    참가자
     화면 캡처 2023-07-09 004053
    connect 밑에 위치할경우 정상적으로 출력이 되지만
    화면 캡처 2023-07-09 003931
    이 코드 가장 아래에 위치할 경우에는 제대로 출력이 되지 않습니다
    이유가 뭔가요?
    
    
    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');
    var db;
    MongoClient.connect('mongodb+srv://greatcloud13:tmdghks8010@cluster0.twp1okv.mongodb.net/?retryWrites=true&w=majority', function (err, client) {
        if (err) return console.log(err)
        db = client.db('todoapp');
        app.listen(8080, function () {
            console.log('listening on 8080')
        })
    });
    app.get('/list', function(요청, 응답){
        db.collection('post').find().toArray(function(에러, 결과){
          console.log(결과)
          응답.render('list.ejs', { posts : 결과 })
        })
    })
     
    app.get('/pet', function (req, res) {
        res.send('Pet toy page')
    });
    app.get('/beauty', (req, res) => {
        res.send('Beauty page')
    });
    app.get('/', function (req, res) {
        res.sendFile(__dirname + '/index.html')
    });
    app.get('/write', function (req, res) {
        res.sendFile(__dirname + '/write.html')
    });
    app.get('/list', function (req, res) {
        res.render('list.ejs')
    });
    app.post('/add', function (req, res) {
        res.send('Send complete')
        db.collection('post').insertOne({ name: req.body.title, date:req.body.date }, function (err, result) {
            console.log('Save complete')
        });
    });
    코드도 함께 첨부합니다
    
     
    
    
    
    
    		
    	
    #90157

    codingapple
    키 마스터
    MongoClient.connect 부터 해놔야 db기능 이용가능합니다 
    res.send는 insertOne 콜백함수 안으로 넣읍시다
    #90173

    차승환
    참가자
    두경우 모두 MongoClient.connect 아래에 위치합니다
    혹시 get요청이 커넥트 바로 아래에 있어야 정상적으로 작동된다는 말씀이신가요?
    
    
    #90201

    codingapple
    키 마스터
    app.get('/list' 가 2개 있는거같은데 하나는 지워야할듯요
    #90227

    차승환
    참가자
    감사합니다
5 글 보임 - 1 에서 5 까지 (총 5 중에서)
  • 답변은 로그인 후 가능합니다.

About

현재 월 700명 신규수강중입니다.

  (09:00~20:00) 빠른 상담은 카톡 플러스친구 코딩애플 (링크)
  admin@codingapple.com
  이용약관
ⓒ Codingapple, 강의 예제, 영상 복제 금지
top

© Codingapple, All rights reserved. 슈퍼로켓 에듀케이션 / 서울특별시 강동구 고덕로 19길 30 / 사업자등록번호 : 212-26-14752 온라인 교육학원업 / 통신판매업신고번호 : 제 2017-서울강동-0002 호 / 개인정보관리자 : 박종흠