发布猫鼬复杂模式

时间:2019-03-19 16:49:51

标签: javascript node.js mongodb mongoose schema

你好

我有这个猫鼬模式(我知道那里有问题),重要的一点是它的“ 区域”部分。

    const mongoose = require('mongoose');

    const destination = new mongoose.Schema({
      name: { type: String, required: true }, 
      flag: String,
      creationDate: { type: Date, default: Date.now },
      region: { id: { type: mongoose.Schema.Types.ObjectId, ref: 'Region' }, name: String },
}, {strict: true});

module.exports = mongoose.model('Destination', destination);

我正在使用以下表单发布

<form action="/destinations" method="post">

  <div class="form-group">
    <label for="destination[name]">Name</label>
    <input class="form-control" type="text" name="destination[name]" placeholder="Destination name...">
  </div>
  <div class="form-group">
    <label for="destination[name]">Flag</label>
    <input class="form-control" type="text" name="destination[flag]" placeholder="Destination flag...">
  </div>
  <div class="form-group">
    <label for="destination[region]">Region</label>
    <select class="form-control mb-4" name="region[name]]">
      <option selected disabled hidden>Choose a region</option>
      <% allRegions.forEach(region => { %>
        <option value="<%= region.name %>">
          <%= region.name %>
        </option>
        <% }); %>
    </select>
  </div>
  <button class="btn btn-primary btn-block" type="submit">Add</button>

</form>

所有数据都正确地发布到处理它的Node.js文件中,但是我找不到保存“区域ID”的方法,这就是现在的处理方式:

exports.destinations_create = (req, res) => {
  req.body.destination.region = { name: req.body.region.name };
  Destination.create(req.body.destination)
  .then(newDestination => {
    Regions.findOne({name: req.body.region.name})
    .then(foundRegion => {
      newDestination.region.id = foundRegion._id;
      foundRegion.countries.push(newDestination._id);
      foundRegion.save();
    });
    res.redirect('/destinations');
  })
  .catch(err => res.redirect('/'));
}

我以为我可以将id保留为空,然后再添加,因为它只是一个对象,但没有任何效果,对这里出什么问题有任何想法吗?

谢谢!

2 个答案:

答案 0 :(得分:0)

由于要引用Region,因此可以保留Region的ID以及name属性。

 const mongoose = require('mongoose');

     const destination = new mongoose.Schema({
      name: { type: String, required: true }, 
      flag: String,
      creationDate: { type: Date, default: Date.now },
      region: {type: mongoose.Schema.Types.ObjectId, ref: 'Region' });
      module.exports = mongoose.model('Destination', destination);

,然后在您的添加方法中:

     exports.destination_create = async(req,res)=>{
    const region = await Region.findOne({name: req.body.region});
    const destination = new Destination({
     region: region._id,
    name: req.body.name,
    flag: req.body.flag
})
await destination.save();
res.redirect('/destination');
 }

答案 1 :(得分:0)

我有点找到解决方案,之所以说是因为我不确定它在内存方面的效率如何,但是:

模型目的地已更改:

region: { type: mongoose.Schema.Types.ObjectId, ref: 'Region' },

然后我要做的是“ 深度填充”区域查询:

Regions.find({})
  .select('_id name image countries')
  .populate({
    path: 'countries',
    model: 'Destination',
    populate: {
      path: 'cities',
      model: 'City'
    }
  }).then(...).catch(...);