在mongodb数据库上发布时输出奇怪

时间:2013-10-28 21:33:17

标签: node.js mongodb

我的数据架构存在问题。我正在用mongodb和node.js构建一个应用程序,使用mongoose这里是我的架构:

var Schema = mongoose.Schema;

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

var Quartiers = new Schema ({
  nomquartier: {type: String},
  listerue: [rue],
  critere: [misc]
})

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


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

这是应用代码:

//LIST PRODUCTS
app.get('/api/products', function (req, res){
  return ObjModel.find(function (err, products){
    if (!err) {
      return res.send(products);
    } else {
      return console.log(err);
    }
  });
});

//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,
    quartiers: [Quartiers],
    critere: [misc],
    rue: [rue]
  });
  product.save(function(err){
    if (!err) {
      return console.log("created");
    } else {
      return console.log(err);
    }
  });
  return res.send(product);
});

//READ SINGLE ELEMENT
app.get('/api/products/:id', function(req,res){
  return ObjModel.findById(req.params.id, function(err, product) {
    if (!err) {
      return res.send(product);
    } else {
      console.log(err);
    }
    return res.send(product);
  });
});

//UPDATE METHOD

app.put('/api/products/:id', function(req,res){
  return ObjModel.findById(req.params.id, function(err, product){
    product.nomville = req.body.nomville;
    product.description = req.body.description;
    product.quartiers= req.body.quartiers;
    return product.save(function(err){
      if (!err) {
        console.log("updated");
      } else {
        console.log(err);
      }
      return res.send(product);
    });
  });
});

//DELETE METHOD

app.delete('/api/products/:id', function(req,res){
  return ObjModel.findById(req.params.id, function(err, product){
    return product.remove(function(err){
      if (!err) {
        console.log("removed");
        return res.send(' ');
      } else {
        console.log(err);
      }
    });
  });
});

并且我将以下Jquery查询发送到数据库:

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

但是如果终端输出正确,我输出的输出不完整,数据库会显示以下输出:编辑这就是我用你的代码Samy所得到的

[
  {
    "nomville": "Strasbourg",
    "description": "Ville de Strasbourg",
    "_id": "526ee45a8d29774913000004",
    "__v": 0,
    "modified": "2013-10-28T22:25:30.970Z",
    "quartiers": []
  }
]

您可以在此处找到我的原始帖子,其中包含其他问题:Error: key $conditionalHandlers must not start with '$' mongodb它有一些额外的代码

2 个答案:

答案 0 :(得分:0)

请为您的apis视图提供代码(查看/ api / products)。 编辑:ok Error: key $conditionalHandlers must not start with '$' mongodb

首先在保存完成后放入res.send,如下所示:

app.post('/api/products', function(req, res, next){
  var product;
  console.log("POST: ");
  console.log(req.body);
  product = new ObjModel({
    nomville: req.body.nomville,
    description: req.body.description,
    quartier: req.body.quartier,
    critere: req.body.critere,
    rue: req.body.rue,
  });
  product.save(function(err){
    if (err) return next(err);
    res.send(product);
  });
});

这是什么(因为它不起作用):

quartiers: [Quartiers],
critere: [misc],
rue: [rue]

答案 1 :(得分:0)

我发现了问题:

嵌入式模式必须具有以下形式:

var misc = new Schema ({
   publictransp: Number, 
   culture: Number,
   nature: Number,
   [ etc...]
})