데이터베이스에 개체를 저장하려고합니다. 나는 데이터베이스가 활성화되어 있다는 것을 알고 있습니다. 왜냐하면 객체를 업데이트하기 위해 동일한 연결을 통해 객체를 읽었 기 때문입니다.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)가있을 때만 실패합니다.
내가 스키마를 설정하는 데있어 누락 된 것으로 의심 되나, 다음에 무엇을 확인할 것인지 잘 모르겠습니다.
미리 도움을 청하십시오!