我不想把物体留给我的数据库;我知道该数据库很活跃,因为我通过同一链接阅读了该物体,用于更新该数据库。
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
另一项更新:当模型没有说明内容时,更新工作似乎正确。 只是当存在一些特有要素(Rounds,以及各回合和问题)时,事情就失败了。
我怀疑,在建立我的计划时,我有点失踪了,但我不敢确定下一步要检查什么。
事先得到任何帮助!