Я пытаюсь сохранить объект в моей базе данных; Я знаю, что база данных активна, потому что я прочитал объект через это же соединение, которое используется для его обновления.Вызов 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
Что я вижу в том, что вызов, чтобы сохранить() не удается, но передавая пустой объект заблуждается.
Вот моя схема:
# 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
Дальнейшее обновление: если модель не имеет вложенных элементов, обновление появляется для правильной работы. Это просто, когда есть вложенные элементы (Раунды, и внутри Раундов, Вопросы), что вещи терпят неудачу.
Я подозреваю, что мне не хватает в настройке моей схемы, но я не уверен, что проверить дальше.
Заранее благодарим за любую помощь!