Ich bin ziemlich neu zu Mungo/Express. Was ich kämpfe, ist zu versuchen, neue Daten vom HTML-Formular zu aktualisieren und es in Datenbank zu speichern, die Verweise hat. Ich habe ein Geschäftsmodell mit Standortbezug zum Standortmodell. Hier sind die Codes.Update-Datenbank mit mehreren Referenz-Datenbank-Probleme
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 - bearbeiten Routen
//(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);
}
});
});
Das Geschäft (Bus) aktualisiert schön aber bus.location nicht aktualisiert. Geschäftsmodell
//----------------------------------------------------------------------------\\
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);
Location Modell
//----------------------------------------------------------------------------\\
var mongoose = require('mongoose');
//----------------------------------------------------------------------------\\
var locSchema = new mongoose.Schema(
{
street: String,
city: String,
state: String,
zipcode: Number
}
);
//----------------------------------------------------------------------------\\
module.exports = mongoose.model('Location', locSchema);
Willkommen bei Stack Overflow. Was ist der Fehler? Wie ist der Datenfluss? Kannst du deinen Code erklären? Seien Sie immer spezifisch und machen Sie sich vor der Anfrage die größte Mühe, und behandeln Sie Stack Overflow nicht als Repository für Lernprogramme. Stack Overflow ist eine Frage-Antwort-Website, kein Code-Schreibdienst. Bitte [siehe hier] (https://stackoverflow.com/help/how-to-ask) um zu erfahren, wie man effektive Fragen schreibt. – Teocci