错误:键$ conditionalHandlers不能以'$'mongodb开头 [英] Error: key $conditionalHandlers must not start with '$' mongodb

查看:149
本文介绍了错误:键$ conditionalHandlers不能以'$'mongodb开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了mongodb的问题,尤其是node.js的mongoose软件包.我有以下架构:

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

当我尝试使用Jquery查询发布此架构的对象时,我得到了这个信息:

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

[错误:键$ conditionalHandlers不能以'$'开头]

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

有什么主意吗?我完全受不了这个.

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

这是发布功能:

//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);
});

这是查询

 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": []
     }
    ]
  }
]

这正常吗?

推荐答案

在定义任何一个架构之前,您要在location架构定义中引用criteremisc.这会导致某种程度的神秘错误,这可能是由于模式定义中意外的undefined值,导致猫鼬将无效查询拼凑在一起的原因.

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],
});

这篇关于错误:键$ conditionalHandlers不能以'$'mongodb开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆