我對貓鼬/快車相當陌生。我正在努力嘗試更新來自HTML表單的新數據並將其保存到具有引用的數據庫。我有一個帶位置模型位置參考的商業模型。這是代碼。更新帶有多個參考數據庫問題的數據庫
edit.ejs
<div class="container">
<div class="form-container">
<form action="/<%= bus._id %>?_method=PUT" method="POST">
<!-- business info -->
<h3>Business Information</h3>
<input class="form-input" type="input" name="bus[logo]" value="<%= bus.logo %>">
<input class="form-input" type="input" name="bus[name]" value="<%= bus.name %>">
<% bus.location.forEach(function(location) { %>
<input class="form-input" type="input" name="bus.location[street]" value="<%= location.street %>">
<input class="form-input" type="input" name="bus.location[city]" value="<%= location.city %>">
<div class="state-input">
<select class="form-inline" name="bus.location[state]">
<option value="" disabled selected><%= location.state %></option>
<option value="AL">Alabama</option>
...
<option value="WY">Wyoming</option>
</select>
</div><!--State-input -->
<input class="form-inline" type="input" name="bus.location[zipcode]" value="<%= location.zipcode %>">
<% }) %>
<!--Contact info-->
<h4>Contact Information</h4>
<% bus.contact.forEach(function(contact) { %>
<input class="form-input" type="url" name="bus[url]" value="<%= bus.url %>">
<input class="form-input" type="email" name="bus.contact[email]" value="<%= contact.email %>">
<input class="form-input" type="tel" name="bus.contact[phone]" value="<%= contact.phone %>">
<input class="form-input" type="input" name="bus.contact[twitter]" value= "<%= contact.twitter %>">
<input class="form-input" type="input" name="bus.contact[facebook]" value="<%= contact.facebook %>">
<input class="form-input" type="input" name="bus.contact[instagram]" value="<%= contact.instagram %>">
<% }) %>
index.js - 編輯路線
//(edit.ejs) Edit Route
app.get('/:id/edit', function(req, res) {
Business.findById(req.params.id)
.populate('location')
.populate('contact')
.populate('images')
.exec(function(err, bus) {
if(err) {
console.log(err);
} else {
res.render('edit', {bus:bus});
}
});
});
app.put('/:id', function(req, res) {
Business.findByIdAndUpdate(req.params.id, req.body.bus, function(err, bus) {
if(err) {
console.log(err);
res.redirect('/' + req.params.id + '/edit');
} else {
res.redirect('/' + req.params.id);
}
});
});
業務(公交車)更新很好,但bus.location不更新。 商業模式
//----------------------------------------------------------------------------\\
var mongoose = require('mongoose');
//----------------------------------------------------------------------------\\
var busSchema = new mongoose.Schema({
name: String,
type: String,
logo: String,
desc: String,
subs: Number,
video: String,
url: String,
firstRun: Boolean,
location:[
{
type: mongoose.Schema.Types.ObjectId,
ref: 'Location'
}
],
contact:[
{
type: mongoose.Schema.Types.ObjectId,
ref: 'Contact'
}
],
images:[
{
type: mongoose.Schema.Types.ObjectId,
ref: 'Image'
}
],
comments:[
{
type: mongoose.Schema.Types.ObjectId,
ref: 'Comment'
}
],
created: {
type: Date, default: Date.now
}
});
//----------------------------------------------------------------------------\\
module.exports = mongoose.model('Business', busSchema);
選址模型
//----------------------------------------------------------------------------\\
var mongoose = require('mongoose');
//----------------------------------------------------------------------------\\
var locSchema = new mongoose.Schema(
{
street: String,
city: String,
state: String,
zipcode: Number
}
);
//----------------------------------------------------------------------------\\
module.exports = mongoose.model('Location', locSchema);
歡迎來到Stack Overflow。什麼是錯誤?數據如何流動?你能解釋你的代碼嗎?在提問之前,始終要具體並盡力而爲,並且不要將Stack Overflow當作教程的存儲庫。堆棧溢出是一個問答網站,而不是代碼寫入服務。請[見這裏](https://stackoverflow.com/help/how-to-ask)學習如何編寫有效的問題。 – Teocci