Привет
У меня есть эта схема Мангуста (я знаю, что там что-то не так), важный бит - это часть " region "это.
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, который их обрабатывает, однако я не могу найти способ сохранить «идентификатор региона», вот каксейчас обрабатывается:
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('/'));
}
Я думал, что мог бы оставить идентификатор пустым, а затем добавить его позже, так как это просто объект, но ничего не работает, есть идеи, что здесь не так?
Заранее спасибо!