MongoDB引用不保存(猫鼬)

时间:2016-07-19 09:50:14

标签: javascript node.js mongodb curl mongoose

所以我遇到了一个令我疯狂的奇怪问题。我有一个商店的架构,代表店面,带有某些制造商的产品,我也有一个架构。 Store架构的一个属性是ObjectId引用的Manufacturer对象数组。

基本上,我想要实现的是,当创建一个新的Store文档时,我想查看所有现有的Manufacturer文档,并将每个文档的_id属性添加到参考数组中。存储文件。

最奇怪的是,在将所有.save()推入阵列后,在新创建的Store模型上调用_id方法之后,调试打印输出显示结果完全保存了我想要的方式。但是,当我进行cURL调用以从我的数据库中提取记录时,制造商数组完全是空的,并且没有任何ObjectId被填充其参考。

我不知道从哪里开始,有人可以帮忙吗?

由于

架构定义:

var Schema = mongoose.Schema;

//create a schema for database
var storeSchema = new Schema({
  "number": String,
  "state": String,
  "zip": String,
  "city": String,
  "street": String,
  "manufacturers": [{
    "type": mongoose.Schema.Types.Mixed,
    "ref": "Manufacturer"
  }]
});

var manufacturerSchema = new Schema({
  "name": String,
  "country": String,
  "products": [{
    "type": mongoose.Schema.Types.ObjectId,
    "ref": "Product"
  }]
});

var Product = mongoose.model("Product", productSchema);
var Manufac = mongoose.model("Manufacturer", manufacturerSchema);

创建商店文档:

app.put("/api/v1.0/stores", function(req, res){
  console.log("PUT request received at /stores.");
  if(Object.keys(req.body).length < 5){
     //not enough parameters
    console.log("Not enough parameters were supplied.");
    res.sendStatus(400);
  }
  //create a new database product document and save to database
  var savedID;
  var params = req.body;
  var tmpStore = new Store({
    "number": params.number,
    "state": params.state,
    "zip": params.zip,
    "city": params.city,
    "street": params.street
  });
  //find all manufacturers 
  Manufac.find({}, function(err, result){
    result.forEach(function(value){
      tmpStore.manufacturers.push(value._id);
    });
  });

  tmpStore.save(function(err, result){
    if(err) {
      console.log("Error PUTing store!"); return;
    }
      console.log("Saved successfully: " + result);
  });
  res.sendStatus(200);
});

输出:

App listening on port 8080.
PUT request received at /stores.
Saved successfully: { __v: 0,
  number: '1',
  state: 'OR',
  zip: '97333',
  city: 'Corvallis',
  street: '1680 Washington St.',
  _id: 578df6a679e28aea6b84bec8,
  manufacturers:
   [ 578dae80a8f8ec3266a5d956,
     578dd1918caa17b467b7caee,
     578dd19f8caa17b467b7caef,
     578dd1bd8caa17b467b7caf0 ] }

来自cURL GET的JSON

{
    "_id": "578df6a679e28aea6b84bec8",
    "number": "1",
    "state": "OR",
    "zip": "97333",
    "city": "Corvallis",
    "street": "1680 Washington St.",
    "__v": 0,
    "manufacturers": []
}

服务器端GET请求代码:

app.get("/api/v1.0/stores", function(req, res) {
  console.log("Get received at /stores");
  Store
      .find({})
      .populate("manufacturers")
      .exec(function(err, result){
        if(err){
          console.log("Error fetching stores!"); return;
        }
        res.json(result);
      });
  });

1 个答案:

答案 0 :(得分:0)

我认为你应该在查找功能中使用tmpStore.save。由于它是异步的,它将在save函数回调之前移至find函数。

我认为你应该有像

这样的东西
Manufac.find({}, function(err, result){
    result.forEach(function(value){
      tmpStore.manufacturers.push(value._id);
    });
tmpStore.save(function(err, result){
    if(err) {
      console.log("Error PUTing store!"); return;
    }
      console.log("Saved successfully: " + result);
  });
  });

我认为这应该有用。