0

オブジェクトをデータベースに保存しようとしています。更新に使用されているのと同じ接続を介してオブジェクトを読み取ったため、データベースがアクティブであることはわかっています。

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)

おそらく、これらの埋め込み要素を間違って扱っているのでしょうか?

興味深いことに、既存の 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

さらに更新: モデルにネストされた要素がない場合、更新は正しく機能しているように見えます。物事が失敗するのは、ネストされた要素 (ラウンド、およびラウンド内の質問) がある場合だけです。

スキーマの設定で何かが欠けているのではないかと思いますが、次に何を確認すればよいかわかりません。

助けてくれてありがとう!

4

1 に答える 1

0

これで解決したと思います。私が保存していた特定の Quiz エンティティは、mongo シェルから直接変更したものでした。

> 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 プロパティのない埋め込まれた Question エンティティが問題だったと思います。すべてのエンティティが _id を持つように、UI を使用してラウンドと質問を使用して新しいクイズを作成すると、うまくいくようです!

于 2012-05-10T17:52:11.947 に答える