Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Mongoose.js Error saving document with unusual but seemingly acceptable string value

Mongoose.js Error saving document with unusual but seemingly acceptable string value

Problem

I'm getting an error saving a document to mongoose.js from Node.js. This code has worked fine for a while but it's now causing a problem with Javascript objects with key values that seem fine otherwise.

What's being saved is a document where one member is an associative array, which for simplicity have represented as Schema.Types.Mixed:

var formatSchema = new mongoose.Schema({
  name: {type: String, required: true},
  values: {type: mongoose.Schema.Types.Mixed, required: false}
});

It's now failing when one object has a key like "0038-". Specifically, it's the dash - that's causing the problem. If I take those values out (for testing, not for user satisfaction) it loads fine. Having isolated it this far am now stumped as to how to proceed.

[
  {
    "name": "INJURYF",
    "values": {
        "0": "No",
        "1": "Yes"
    }
  },
  {
    "name": "RFVF",
    "values": {
        "10050": "Chills",
        "10100": "Fever",
        "10200": "General weakness",
        "10250": "General ill feeling",
        "00323": "Salmonella arthritis",
        "00324": "Salmonella osteomyelitis",
        "0038-": "Other specified salmonella infections",   // PROBLEM STARTS HERE
        "0039-": "Salmonella infection, unspecified",
        "0040-": "Shigella dysenteriae",
        ...
      }
    }, ...
  ]

Here's the specific error message,

Express
500 TypeError: Cannot call method 'path' of undefined
at getPositionalPath (/myproject/node_modules/mongoose/lib/schema.js:380:22)
at Schema.path (/myproject/node_modules/mongoose/lib/schema.js:199:9)
at Query._getSchema (/myproject/node_modules/mongoose/lib/query.js:1789:27)
at Query._walkUpdatePath (/myproject/node_modules/mongoose/lib/query.js:1721:16)
at Query._walkUpdatePath (/myproject/node_modules/mongoose/lib/query.js:1716:25)
at Query._castUpdate (/myproject/node_modules/mongoose/lib/query.js:1655:23)
at castDoc (/myproject/node_modules/mongoose/lib/query.js:2160:18)
at Query._findAndModify (/myproject/node_modules/mongoose/lib/query.js:2032:17)
at Query.findOneAndUpdate (/myproject/node_modules/mongoose/lib/query.js:1941:15)
at Function.Model.findOneAndUpdate (/myproject/node_modules/mongoose/lib/model.js:1127:16)

Mongoose positional operator errors

Problem courtesy of: prototype

Solution

Here we go:

This is a bug of an older mongoose version - please update to the latest version.

Solution courtesy of: matthias

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Mongoose.js Error saving document with unusual but seemingly acceptable string value

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×