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

home2 게시판 Node.js, Express 게시판 updateOne 관련 질문입니다.

updateOne 관련 질문입니다.

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

    이윤서
    참가자
    제가 구현하고 싶은 기능은 특정 수를 입력해서 post요청을 하면, 그 수가 DB에 점차 더해지며 저장되는 기능을 구현하려하고 있습니다.
    예를 들어 DB에 '포인트 : 6' 이라는 값이 저장되어 있는 상태에서 3이라는 숫자를 post요청하면, DB에 있는 값이 '포인트 : 9'가 되도록 하고 싶습니다.
    
    
    app.post('/finish', 로그인했니, async (요청, 응답) => {
        console.log(요청.body)
       
        await db.collection('goodhistory').insertOne({ title: 요청.body.title, content: 요청.body.content, important: 요청.body.important, category: 요청.body.category, time: 요청.body.time, submissionDate: 요청.body.submissionDate, writer: 요청.user.fullname })
        
        await db.collection('goals').deleteOne({ _id : new ObjectId
            (요청.body.id) })
        await db.collection('user').updateOne({$inc: { mileage : +요청.body.important}})
        
           
        응답.redirect('/goals')
    })
    
    유저라는 DB에 기존 유저의 마일리지가 저장되어 있고, 여기에 '요청.body.important'를 더해주고 싶은데 어떻게 해야 할지 모르겠습니다.. 위 코드는 server.js 입니다..
    #102761

    codingapple
    키 마스터
    .updateOne({ 찾을document정보 }, {$inc: { mileage : +요청.body.important}}) 라고 써야할듯요
    #102770

    이윤서
    참가자
    찾을 document 정보에는 뭘 넣어야할지..'user'DB에 mileage에 더한다는 내용이 다 들어가있는데 찾을 document 정보에는 뭘 넣어야하나요?
    g현재
    
    app.post('/finish', 로그인했니, async (요청, 응답) => {
        console.log(요청.body)
       
        await db.collection('goodhistory').insertOne({ title: 요청.body.title, content: 요청.body.content, important: 요청.body.important, category: 요청.body.category, time: 요청.body.time, submissionDate: 요청.body.submissionDate, writer: 요청.user.fullname })
        
        await db.collection('goals').deleteOne({ _id : new ObjectId
            (요청.body.id) })
        await db.collection('user').updateOne({ mileage : 요청.user.mileage }, {$inc: { mileage : +요청.body.important}})  
        응답.redirect('/goals')
    })
    
    이렇게 넣고 실행했을 때  return callback(new error_1.MongoServerError(res.writeErrors[0])); 이런 에러가 났습니다..
    #102799

    codingapple
    키 마스터
    { _id : 수정하고싶은 document의 _id } 이런거 넣읍시다
    #103434

    이윤서
    참가자
     await db.collection('user').updateOne({ _id: new ObjectId(요청.user._id)}, {$inc: { mileage : + 요청.body.important}})
    
    로 수정해서 하고 있습니다. 근데도 계속 안되고 있는데..
    
    요청.body.important에 들어가는 값이 
     <select class="form-control" id="exampleFormControlSelect1" name="important" required>
              <option>1</option>
              <option>2</option>
              <option>3</option>
              <option>4</option>
              <option>5</option>
            </select>
    
    여기서 선택한 값이 들어가는건데 여기에는 문제가 없나요..? 저기서 고른 숫자가 더해졌으면 하는데..
    #103467

    codingapple
    키 마스터
    넴 문자말고 숫자자료로 바꿔서 넣어봅시다
6 글 보임 - 1 에서 6 까지 (총 6 중에서)
  • 답변은 로그인 후 가능합니다.

About

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

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

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