5 글 보임 - 1 에서 5 까지 (총 5 중에서)
-
글쓴이글
-
2022년 11월 19일 21:30 #54756
이창민참가자TypeError: Cannot read properties of undefined (reading '_id') 해당 에러가 뜨는 이유가 해석이 안됩니다ㅠㅠ 왜뜨는건가욤 어디가 틀린건지 ㅠㅠ
//채팅기능 app.post('/chatroom', 로그인했니, function(요청, 응답){
var 저장할거 = { title : '무슨무슨채팅방', member : [ObjectId(요청.body.당한사람id), 요청.user._id], date : new Date() }
db.collection('chatroom').insertOne(저장할거).then((결과)=>{ 응답.send('성공') }) })
app.get('/chat', function(요청, 응답){ db.collection('chatroom').find({ member : 요청.user._id }).toArray().then((결과)=>{ 응답.render('chat.ejs', { data : 결과 }) }) })
app.post('/message', 로그인했니, function(요청, 응답){ var 저장할거 = { parent : 요청.body.parent, content : 요청.body.content, userid : 요청.user._id, date : new Date(), } db.collection('message').insertOne(저장할거).then(()=>{ console.log('DB저장성공') 응답.send('DB저장성공') }) })
app.get('/message/:id', 로그인했니, function(요청, 응답){
응답.writeHead(200, { "Connection": "keep-alive", "Content-Type": "text/event-stream", "Cache-Control": "no-cache", }); db.collection('message').find({ parent : 요청.params.id }).toArray().then((결과)=>{ 응답.write('event: test\n'); 응답.write('data: ' + JSON.stringify(결과) + '\n\n'); })
const pipeline = [ { $match: { 'fullDocument.parent' : 요청.params.id } } ]; const collection = db.collection('message'); const changeStream = db.collection.watch(pipeline); changeStream.on('change', (result) => { 응답.write('event: test\n'); 응답.write('data: ' + JSON.stringify([result.fullDocument]) + '\n\n'); }); }); -----------------------
<script> var 지금누른채팅방id; var eventSource; $('.list-group-item').click(function(){ 지금누른채팅방id = this.dataset.id; $('.chat-content').html('');
if(eventSource != undefined){ eventSource.close() }
eventSource = new EventSource('/message' + 지금누른채팅방id) eventSource.addEventListener('test', function(e){ var 가져온거 = JSON.parse(e.data); 가져온거.forEach(function(i){ $('.chat-content').append('
- <span class="chat-box">' + i.content + '</span>
') }) }) })$('#send').click(function(){ var 채팅내용 = $('#chat-input').val(); var 보낼거 = { parent : 지금누른채팅방id, content : 채팅내용, } $.post('/message', 보낼거).then(()=>{ console.log('전송성공') }) }) </script>
2022년 11월 19일 21:35 #54757
이창민참가자app.get('/message/:id', 로그인했니, function(요청, 응답){ 저기가 틀렸다는데 왜 틀린건지 모르겠습니다 ㅠ
2022년 11월 20일 18:07 #54818
codingapple키 마스터넴 밑에서 params.id 라고 쓰고싶으면 :id라고 쓰고 params.parentid라고 쓰고싶으면 :parentid라고 쓰면 됩니다
-
글쓴이글
5 글 보임 - 1 에서 5 까지 (총 5 중에서)
- 답변은 로그인 후 가능합니다.