2012-05-09 5 views
0

데이터베이스에 개체를 저장하려고합니다. 나는 데이터베이스가 활성화되어 있다는 것을 알고 있습니다. 왜냐하면 객체를 업데이트하기 위해 동일한 연결을 통해 객체를 읽었 기 때문입니다.Mongoose document.save()를 호출해도 저장되지 않습니까?

saveAndReturnQuiz = (res, quiz) -> 
    quiz.save (err) -> 
    # At some point, started seeing empty object for err, rather than null 
    if not _.isEmpty err 
     switch err?.code 
     when 11000 
      sendError res, "Quiz title must be unique" 
     else 
      console.error "Unexpected error on save(): %j", err 
      sendError res, extractError err 

     return 

    console.log "Sending Quiz response: %j", quiz 

    res.send quiz 

내가보기에 save() 호출이 실패했지만 빈 err 객체를 전달하고 있습니다. 아마도 내가 그냥 잘못이 포함 된 요소를 다루는거야 그래서

# Defines the schema of data 
# Exports three Mongoose Model objects: Question, Round, and Quiz 

mongoose = require "mongoose" 

Schema = mongoose.Schema 

Question = new Schema 
    kind: 
    type: String 
    enum: [ "text" ] 
    text: 
    type: String 
    required: true 
    answer: 
    type: String 
    required: true 
    value: Number 
    { strict: true } 

Round = new Schema 
    kind: 
    type: String 
    required: true 
    enum: ["normal", "challenge", "wager"] 
    title: 
    type: String 
    required: true 
    questions: [Question] 
    { strict: true } 

Quiz = new Schema 
    title: 
    type: String 
    required: true 
    unique: true 
    created: 
    type: Date 
    default: -> new Date() 
    location: String 
    rounds: [Round] 
    { strict: true } 

module.exports = 
    Question: mongoose.model('Question', Question) 
    Round: mongoose.model('Round', Round) 
    Quiz: mongoose.model('Quiz', Quiz) 

:

여기 내 스키마입니까?

흥미롭게도, 난 그냥 기존 업데이트하지 데이터베이스에 새 퀴즈 개체를 추가 할 수 있습니다 :

app.post "/api/quiz", 
    (req, res) -> 
     quiz = new Quiz req.body 
     saveAndReturnQuiz res, quiz 

    # Update an existing Quiz 
    app.put "/api/quiz/:id", 
    (req, res) -> 
     Quiz.findById req.params.id, 
     handleError res, (quiz) -> 
      _.extend quiz, req.body 
      console.log "Ready to save: %j", quiz 
      saveAndReturnQuiz res, quiz 

또 갱신 : 모델이 더 중첩 된 요소가없는 경우, 업데이트가 제대로 작동하려면 나타납니다. 그것은 중첩 된 요소 (Rounds, Rounds, Questions)가있을 때만 실패합니다.

내가 스키마를 설정하는 데있어 누락 된 것으로 의심 되나, 다음에 무엇을 확인할 것인지 잘 모르겠습니다.

미리 도움을 청하십시오!

답변

0

나는 이것을 풀 었다고 믿는다;

> db.quizzes.find({title: "NFJS" }).pretty() 
{ 
    "_id" : ObjectId("4f835669c34c2a9c6f000003"), 
    "created" : ISODate("2012-04-09T21:36:41.726Z"), 
    "location" : "San Antonio", 
    "rounds" : [ 
    { 
     "_id" : ObjectId("4f835669c34c2a9c6f000004"), 
     "kind" : "normal", 
     "questions" : [ 
     { 
      "kind" : "text", 
      "answer" : "My Answer", 
      "value" : 10, 
      "text" : "My Question" 
     } 
     ], 
     "title" : "Server-Side Java and JavaScript" 
    }, 
    { 
     "kind" : "normal", 
     "title" : "Underscore/Bootstrap Trivia", 
     "_id" : ObjectId("4fa317319b19aca4c900004c"), 
     "questions" : [ ] 
    } 
    ], 
    "title" : "NFJS" 
} 

내가 더 _id를하게 proprty으로 문제를 포함 질문 엔티티를했다 보라 : 내가 저장 한 특정 퀴즈 엔티티 내가 몽고 쉘에서 직접 수정 한 하나였다. 내 UI를 통해 라운드와 질문으로 새로운 퀴즈 만들기, 모든 엔티티에 _id가 있으므로 제대로 작동하는 것 같습니다!

관련 문제