English 中文(简体)
援引Mongoose文件似乎并不节省?
原标题:Invoking Mongoose document.save() does not appear to 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

另一项更新:当模型没有说明内容时,更新工作似乎正确。 只是当存在一些特有要素(Rounds,以及各回合和问题)时,事情就失败了。

我怀疑,在建立我的计划时,我有点失踪了,但我不敢确定下一步要检查什么。

事先得到任何帮助!

最佳回答

我认为,我已经解决了这一问题;我所挽救的基茨实体是我直接修改的。

> 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"
}

我是问题所在,没有吗? 创建新的基茨,通过我的“倡议”进行回合和提问,以便每个实体都有其——看来是出色的!

问题回答

暂无回答




相关问题
How to make Sequelize use singular table names

I have an model called User but Sequelize looks for the table USERS whenever I am trying to save in the DB. Does anyone know how to set Sequelize to use singular table names? Thanks.

What is Node.js? [closed]

I don t fully get what Node.js is all about. Maybe it s because I am mainly a web based business application developer. What is it and what is the use of it? My understanding so far is that: The ...

Clientside going serverside with node.js

I`ve been looking for a serverside language for some time, and python got my attention somewhat. But as I already know and love javascript, I now want learn to code on the server with js and node.js. ...

Can I use jQuery with Node.js?

Is it possible to use jQuery selectors/DOM manipulation on the server-side using Node.js?

How do I escape a string for a shell command in node?

In nodejs, the only way to execute external commands is via sys.exec(cmd). I d like to call an external command and give it data via stdin. In nodejs there does yet not appear to be a way to open a ...

热门标签