我一直试图将DOM对象附加到Mongo数据库的Sub文档中的对象列表中。但是,到目前为止没有用。我从html表单中检索数据,在该表单中我将数据分组为对象。我首先尝试使用forEach循环,但是我不断收到一条消息,提示$ addToSet为空,尽管当我登录从DOM检索的对象时似乎也不为空。我采用了相同的方法,在前一条路由的放置请求中,然后更新数据库没有问题。这是我的第一个应用程序,我仍然是菜鸟,因此非常感谢您的帮助。
// my database Schema
// schema for images
var images = new Schema(
{
title: String,
description: String,
link: String
}
);
// shema lower for the tes dictionary
var days = new Schema({
day: Number,
route: String,
distance: Number,
description: String,
imageamount: Number,
images: Array(images),
})
// schema upper for the test dictionary
var tourSchema = new Schema({
date: String,
thumbnail: String,
categories: Array,
title: String,
locations: Array,
duration: Number,
generalDescription: String,
dayInfo: Array(days),
});
var Tour = mongoose.model("Tour", tourSchema);
// this is the code Im using to update the db
app.put("/tours/:id/dayinfo/images", (req, res) => {
Tour.findById(req.params.id, (err, tour) => {
if (err) {
console.log("error");
} else {
for( var i = 0; i < tour.dayInfo.length; i++) {
for (var j = 1; j <= tour.dayInfo[i].imageamount; j++){
Tour.updateOne({_id: tour.dayInfo[i]._id}, {$addToSet: { images: req.body["images" + String(Number(i+1)) + String(j)] } },{upsert: true}, (err, updatedImages) => {
if (err) {
console.log(err);
} else {
console.log(updatedImages);
}
})
}
console.log("updated imagess for images ");
};
res.redirect("/");
}
})
});
<form class="some-form" action="/tours/<%= tour._id %>/dayinfo/images?_method=PUT" method="POST">
<% tour.dayInfo.forEach ( (day) => { %>
<header>
<h3>Images of <b>Day <%= day.day %><b></h3>
</header>
<hr>
<% for (var i = 1; i <= day.imageamount; i++ ) { %>
<!-- tour info -->
<div class="row">
<div class="col-md-6">
<input type="text" name="images<%= day.day %><%= i %>[title]" value="" placeholder="Short title of image" class="form-control">
</div>
<div class="col-md-6">
<input type="text" name="images<%= day.day %><%= i %>[description]" value="" placeholder="Short description of image" class="form-control">
</div>
</div>
<br>
<input type="text" name="images<%= day.day %><%= i %>[link]" value="" placeholder="link of image" class="form-control">
<br>
<% } %>
<% }); %>
<input type="submit" name="" value="SAVE" class="btn btn-success btn-block">
</form>
// this code seems to work, yet when applying the same approach for a deeper laying doc, it does not work i get a message saying that '\'addToSet'\' is empty.
/ update({_id: ObjectId("5bf404f0fd415e077e849694")}, {$addToSet: {dayInfo: {day: 8} }}, {upsert: true} )
app.put("/tours/:id/dayinfo", (req, res) => {
Tour.findById(req.params.id, (err, tour) => {
if (err) {
console.log(err)
} else {
for (var i = 1; i <= tour.duration; i++){
Tour.update({_id: req.params.id}, {$addToSet: { dayInfo: req.body["dayInfo" + String(i)] } }, {upsert: true}, (err, updatedDayInfo) => {
if (err) {
console.log(err);
} else {
console.log(req.body["dayInfo" + String(i)]);
}
})
console.log("successfully updated day " + i );
}
}
})
res.redirect("/tours/" + req.params.id + "/dayinfo/images" );
});