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

Error: key $conditionalHandlers must not start with '$' mongodb

Error: key $conditionalHandlers must not start with '$' mongodb

Problem

I have an issue with mongodb and especially the mongoose package for node.js. I have the following schema :

var Schema = mongoose.Schema;

var location = new Schema ({
  nomville: {type: String, required: true},
  description: {type: String, required: true},
  quartier: [quartier],
  critere: [misc],
  modified: {type: Date, default: Date.now}
});

var misc = new Schema ({
  publictransp: {type: Number},
  culture: {type: Number},
  traffic: {type: Number},
  nature: {type: Number},
  supermarket: {type: Number},
  school: {type: Number},
  sport: {type: Number},
  nightlife: {type: Number},
  mean: {type: Number}
});

var rue = new Schema ({
  nomrue: {type: String},
  modified: {type: Date, default: Date.now}
});

var quartier = new Schema ({
  nomquartier: {type: String},
  listerue: [rue],
  modified: {type: Date, default: Date.now}
})

var ObjModel = mongoose.model('Obj',location);

And when I try to post an object of this schema with a Jquery query I get this :

[Error: key $conditionalHandlers must not start with '$']

Any idea ? I'm completely stuck with this one.

EDIT1 : here's the post function :

//CREATE PRODUCTS
app.post('/api/products', function(req,res){
  var product;
  console.log("POST: ");
  console.log(req.body);
  product = new ObjModel({
    nomville: req.body.nomville,
    description: req.body.description,
    quartier: [quartier],
    critere: [misc],
  });
  product.save(function(err){
    if (!err) {
      return console.log("created");
    } else {
   return console.log(err);
    }
  });
  return res.send(product);
});

EDIT2 : here's the query

 jQuery.post("/api/products", {
  "nomville": "Strasbourg",
  "description": "Ville de Strasbourg",
  "quartier": [
    {
      "nomquartier": "Centre",
      "listerue": [
          {
            "nomrue": "22 Novembre"
          },
          {
            "nomrue": "Abattoir"
          },
          {
            "nomrue": "Fonderie"
          },
          {
            "nomrue": "Francs Bourgeois"
          }
      ]
    },
    {
      "nomquartier": "Cité de l'Ill",
      "listerue": [
          {
            "nomrue": "Anguille (chemin de l)"
          },
          {
            "nomrue": "Civelles (promenade des)"
          },
          {
            "nomrue": "Hechner (rue)"
          },
          {
            "nomrue": "Phario (pont)"
          }
      ]
    },
    {
      "nomquartier": "Krutenau",
      "listerue": [
          {
            "nomrue": "Abreuvoir (rue de l)"
          },
          {
            "nomrue": "Bain Finkwiller (rue du)"
          },
          {
            "nomrue": "Fustel de Coulanges (quai)"
          },
          {
           "nomrue": "Hôpital (place de l)"
          }
      ]
    }
  ],
  "critere":[
    {
      "publictransp": 2,
      "culture": 8,
      "traffic": 5,
      "nature": 7,
      "supermarket": 3,
      "school": 5,
      "sport": 6,
      "nightlife": 4,
      "mean": 5
    },
  ]
}, function(data, textStatus, jqXHR) {
    console.log("Post resposne:"); console.dir(data); console.log(textStatus);         console.dir(jqXHR);
});

That's what I get after posting the query:

[
  {
    "nomville": "Strasbourg",
    "description": "Ville de Strasbourg",
    "_id": "526ecdd3a78290010c000004",
    "__v": 0,
    "modified": "2013-10-28T20:49:23.235Z",
    "critere": [],
    "quartiers": [
      {
        "_id": "526ecdd3a78290010c000005",
       "listerue": []
     }
    ]
  }
]

Is this normal ??

Problem courtesy of: MaximeHeckel

Solution

You're referring to critere and misc in the location schema definition before either of those schemas are defined. This is causing that somewhat cryptic error, likely due to mongoose piecing together an invalid query because of the unexpected undefined values in the schema definition.

Beyond this, I don't know what you're trying to do here:

product = new ObjModel({
  nomville: req.body.nomville,
  description: req.body.description,

  // Are the next two lines a mistake?  It appears as though you're trying
  // to create an object using the schemas.
  quartier: [quartier],
  critere: [misc],
});
Solution courtesy of: numbers1311407

Discussion

View additional discussion.



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

Share the post

Error: key $conditionalHandlers must not start with '$' mongodb

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×