home2 ๊ฒŒ์‹œํŒ Node.js, Express ๊ฒŒ์‹œํŒ TypeError: Cannot read properties of undefined (reading 'collection')

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

  • ์ด ์ฃผ์ œ์—๋Š” 1๊ฐœ ๋‹ต๋ณ€, 2๋ช… ์ฐธ์—ฌ๊ฐ€ ์žˆ์œผ๋ฉฐ codingapple๊ฐ€ 1 ๋…„, 11 ์›” ์ „์— ์ „์— ๋งˆ์ง€๋ง‰์œผ๋กœ ์—…๋ฐ์ดํŠธํ–ˆ์Šต๋‹ˆ๋‹ค.
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 ํ˜ธ / ๊ฐœ์ธ์ •๋ณด๊ด€๋ฆฌ์ž : ๋ฐ•์ข…ํ