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

home2 게시판 Node.js, Express 게시판 TypeError: Cannot read properties of undefined (reading 'collection')

TypeError: Cannot read properties of undefined (reading 'collection')

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

    최원호
    참가자
    app.get('/news',(req,res)=>{
       db.collection('post').insertOne({title:'New Jeans'})
       res.send('today is a holiday')
    })
    여기까진 잘 됐는데  그 다음이 안됩니다.
    TypeError: Cannot read properties of undefined (reading 'collection')
    at C:\wonnho\Forum\server.js:40:24
    at Layer.handle [as handle_request] (C:\wonnho\Forum\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\wonnho\Forum\node_modules\express\lib\router\route.js:144:13)
    at Route.dispatch (C:\wonnho\Forum\node_modules\express\lib\router\route.js:114:3)
    at Layer.handle [as handle_request] (C:\wonnho\Forum\node_modules\express\lib\router\layer.js:95:5)
    at C:\wonnho\Forum\node_modules\express\lib\router\index.js:284:15
    at Function.process_params (C:\wonnho\Forum\node_modules\express\lib\router\index.js:346:12)
    at next (C:\wonnho\Forum\node_modules\express\lib\router\index.js:280:10)
    at SendStream.error (C:\wonnho\Forum\node_modules\serve-static\index.js:121:7)
    at SendStream.emit (node:events:514:28)
    
    
    const express=require('express')
    const app=express()
    app.use(express.static(__dirname,+'/public'))
    const {MongoClient}=require('mongodb');
    let db;
    const url='mongodb+srv://:@cluster0.xlmne3z.mongodb.net/?retryWrites=true&w=majority';
    new MongoClient(url).connect().then((client)=>{
       console.log('connected')
       db=client.db('forum');
    }).catch((err)=>{
       console.log(err)
    })
    app.listen(8080,()=>{
       console.log('http://localhost:8080')
    })
    app.get('/',(req,res)=>{
       res.sendFile(__dirname+'/index.html')
    })
    app.get('/',(req,res)=>{
       res.send('start again')
    })
    app.get('/news',(req,res)=>{
       db.collection('post').insertOne({title:'New Jeans'})
       res.send('today is a holiday')
    })
    app.get('/shop',(req,res)=>{
       res.send('opening soom')
    })
    app.get('/list',async(req,res)=>{
       let result=await db.collection('post').find().toArray()
       console.log(result)
       res.send('get document from MongoDB, which is a kind of rows in excel');
    });
    
    
    #112905

    codingapple
    키 마스터
    db.collection 왼쪽에 await 붙입시다
2 글 보임 - 1 에서 2 까지 (총 2 중에서)
  • 답변은 로그인 후 가능합니다.

About

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

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

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