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

home2 게시판 Node.js, Express 게시판 몽고db 저장

몽고db 저장

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

    고대건
    참가자

    const express = require('express');
    const app = express();
    const bodyParser= require('body-parser');
    app.use(bodyParser.urlencoded({extended: true}));
    const MongoClient = require('mongodb').MongoClient;

    var db;

    MongoClient.connect('mongodb+srv://admin:1111@cluster0.5jnwt.mongodb.net/myFirstDatabase?retryWrites=true&w=majority', { useUnifiedTopology: true }, function (에러, client) {
      if (에러) return console.log(에러)
      db = client.db('todoapp');

      db.collection('post').insertOne( {이름 : 'John', 나이: 100} , function(에러, 결과){
        console.log('저장완료');
    });

      app.listen(8080, function () {
        console.log('listening on 8080')
      });
    });

    app.get('/', function(요청, 응답) {
      응답.sendFile(__dirname +'/index.html');
    })

    app.get('/write', function(요청, 응답) {
        응답.sendFile(__dirname +'/write.html');
    });

    app.post('/add', function(요청, 응답){
      console.log(요청.body);
      응답.send('전송완료')
    });

    app.post('/add', function(요청, 응답){
      응답.send('전송완료');
      console.log(요청.body.title);
      console.log(요청.body.date);
      db.collection('post').insertOne( { 제목 : 요청.body.title, 날짜 : 요청.body.date } , function(에러, 결과){
        console.log('저장완료')
      });
      });

     

    -----------------------------------------------------------------------------------------------------------------------------------------

    제가 몽고db에 연결하고 있는데 위에 선언문은 잘 저장이 되는데 왜 아래 있는

    폼에서는 저장이 안되는지 모르겠습니다.  콘솔에도 정상적으로 뜨는데 데이터가 저장이 안되네요 터미널에는 잘 뜹니다.

    db연결도 잘 되었고 form에 name 요소도 모두 적용했습니다

    app.post('/add', function(요청, 응답){
      응답.send('전송완료');
      console.log(요청.body.title);
      console.log(요청.body.date);
      db.collection('post').insertOne( { 제목 : 요청.body.title, 날짜 : 요청.body.date } , function(에러, 결과){
        console.log('저장완료')
      });
      });

    #22903

    고대건
    참가자

    해결했습니다. 

     

    app.post('/add', function(요청, 응답){
      console.log(요청.body);
      응답.send('전송완료')
    });

    이 부분을 지우니 되네요

    #22907

    codingapple
    키 마스터

    app.post('/add' 가 두개나 있었군요 

3 글 보임 - 1 에서 3 까지 (총 3 중에서)
  • 답변은 로그인 후 가능합니다.

About

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

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

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