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');
});