Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7914695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:12:21+00:00 2026-06-03T14:12:21+00:00

I’m attempting to save an object to my database; I know the database is

  • 0

I’m attempting to save an object to my database; I know the database is active, because I’ve read the object in via the same connection being used to update it.

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

What I’m seeing is that the call to save() is failing, but passing an empty err object.

Here’s my schema:

# 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)

So perhaps I’m just dealing with these embedded elements incorrectly?

Interestingly, I can add new Quiz objects to the database, just not update the existing ones:

  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

A further update: when the model has no nested elements, the update appears to work correctly. It is just when there are nested elements (Rounds, and within Rounds, Questions) that things fail.

I suspect there’s something I’m missing in setting up my schema, but I’m not sure what to check next.

Thanks in advance for any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T14:12:22+00:00Added an answer on June 3, 2026 at 2:12 pm

    I believe I have solved this; the particular Quiz entity I was saving was one I had modified directly from mongo shell:

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

    I belive the embedded Question entity with no _id proprty was the problem. Creating a new Quiz with Rounds and Questions through my UI, so that every entity has its _id, seems to work fine!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
i got an object with contents of html markup in it, for example: string
I have a reasonable size flat file database of text documents mostly saved in
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.