Add an object to a sub document list of object Node.js Express
I have been trying to append a DOM object to a list of object into a Sub document of my Mongo database. However, so far no avail. Im retrieving the data from an html form in which i grouped the data in objects. I first tried to use a forEach loop, but i kept getting an message saying that the $addToSet is empty, eventhough when i log the Objects im retrieving from the DOM seems to not be empty. I applied this is same approach, in a put request from a previous route whilst then i had no problem updating the database. This is my first application, im still a noob, so you help would be very much appreciated.
// 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" );
});
node.js mongodb express mongoose
add a comment |
I have been trying to append a DOM object to a list of object into a Sub document of my Mongo database. However, so far no avail. Im retrieving the data from an html form in which i grouped the data in objects. I first tried to use a forEach loop, but i kept getting an message saying that the $addToSet is empty, eventhough when i log the Objects im retrieving from the DOM seems to not be empty. I applied this is same approach, in a put request from a previous route whilst then i had no problem updating the database. This is my first application, im still a noob, so you help would be very much appreciated.
// 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" );
});
node.js mongodb express mongoose
1
@OttoGutierrez That's not the problem. There is afor
loop where the callbacks are not being awaited before it completes. You "can" learn about how to implement things so callbacks or promises are run "serially" or at least respect loop completion. Or you can learn to write all the requests at once withbulkWrite()
. Since the latter means just one call to the database and no need to control multiple async calls, it usually makes most sense to understand that one first. But learning async control also helps.
– Neil Lunn
Nov 21 '18 at 20:15
@OttoGutierrez the list is actually an array
– RasenGUY
Nov 22 '18 at 2:25
@NeilLunn , thank you i did not know this part yet, im going to research and find out how to impolment loops so callback s are run serially
– RasenGUY
Nov 22 '18 at 2:25
add a comment |
I have been trying to append a DOM object to a list of object into a Sub document of my Mongo database. However, so far no avail. Im retrieving the data from an html form in which i grouped the data in objects. I first tried to use a forEach loop, but i kept getting an message saying that the $addToSet is empty, eventhough when i log the Objects im retrieving from the DOM seems to not be empty. I applied this is same approach, in a put request from a previous route whilst then i had no problem updating the database. This is my first application, im still a noob, so you help would be very much appreciated.
// 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" );
});
node.js mongodb express mongoose
I have been trying to append a DOM object to a list of object into a Sub document of my Mongo database. However, so far no avail. Im retrieving the data from an html form in which i grouped the data in objects. I first tried to use a forEach loop, but i kept getting an message saying that the $addToSet is empty, eventhough when i log the Objects im retrieving from the DOM seems to not be empty. I applied this is same approach, in a put request from a previous route whilst then i had no problem updating the database. This is my first application, im still a noob, so you help would be very much appreciated.
// 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" );
});
// 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>
// 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" );
});
// 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" );
});
node.js mongodb express mongoose
node.js mongodb express mongoose
edited Nov 21 '18 at 20:17
Neil Lunn
99.8k23176187
99.8k23176187
asked Nov 21 '18 at 20:01
RasenGUYRasenGUY
113
113
1
@OttoGutierrez That's not the problem. There is afor
loop where the callbacks are not being awaited before it completes. You "can" learn about how to implement things so callbacks or promises are run "serially" or at least respect loop completion. Or you can learn to write all the requests at once withbulkWrite()
. Since the latter means just one call to the database and no need to control multiple async calls, it usually makes most sense to understand that one first. But learning async control also helps.
– Neil Lunn
Nov 21 '18 at 20:15
@OttoGutierrez the list is actually an array
– RasenGUY
Nov 22 '18 at 2:25
@NeilLunn , thank you i did not know this part yet, im going to research and find out how to impolment loops so callback s are run serially
– RasenGUY
Nov 22 '18 at 2:25
add a comment |
1
@OttoGutierrez That's not the problem. There is afor
loop where the callbacks are not being awaited before it completes. You "can" learn about how to implement things so callbacks or promises are run "serially" or at least respect loop completion. Or you can learn to write all the requests at once withbulkWrite()
. Since the latter means just one call to the database and no need to control multiple async calls, it usually makes most sense to understand that one first. But learning async control also helps.
– Neil Lunn
Nov 21 '18 at 20:15
@OttoGutierrez the list is actually an array
– RasenGUY
Nov 22 '18 at 2:25
@NeilLunn , thank you i did not know this part yet, im going to research and find out how to impolment loops so callback s are run serially
– RasenGUY
Nov 22 '18 at 2:25
1
1
@OttoGutierrez That's not the problem. There is a
for
loop where the callbacks are not being awaited before it completes. You "can" learn about how to implement things so callbacks or promises are run "serially" or at least respect loop completion. Or you can learn to write all the requests at once with bulkWrite()
. Since the latter means just one call to the database and no need to control multiple async calls, it usually makes most sense to understand that one first. But learning async control also helps.– Neil Lunn
Nov 21 '18 at 20:15
@OttoGutierrez That's not the problem. There is a
for
loop where the callbacks are not being awaited before it completes. You "can" learn about how to implement things so callbacks or promises are run "serially" or at least respect loop completion. Or you can learn to write all the requests at once with bulkWrite()
. Since the latter means just one call to the database and no need to control multiple async calls, it usually makes most sense to understand that one first. But learning async control also helps.– Neil Lunn
Nov 21 '18 at 20:15
@OttoGutierrez the list is actually an array
– RasenGUY
Nov 22 '18 at 2:25
@OttoGutierrez the list is actually an array
– RasenGUY
Nov 22 '18 at 2:25
@NeilLunn , thank you i did not know this part yet, im going to research and find out how to impolment loops so callback s are run serially
– RasenGUY
Nov 22 '18 at 2:25
@NeilLunn , thank you i did not know this part yet, im going to research and find out how to impolment loops so callback s are run serially
– RasenGUY
Nov 22 '18 at 2:25
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419688%2fadd-an-object-to-a-sub-document-list-of-object-node-js-express%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419688%2fadd-an-object-to-a-sub-document-list-of-object-node-js-express%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
@OttoGutierrez That's not the problem. There is a
for
loop where the callbacks are not being awaited before it completes. You "can" learn about how to implement things so callbacks or promises are run "serially" or at least respect loop completion. Or you can learn to write all the requests at once withbulkWrite()
. Since the latter means just one call to the database and no need to control multiple async calls, it usually makes most sense to understand that one first. But learning async control also helps.– Neil Lunn
Nov 21 '18 at 20:15
@OttoGutierrez the list is actually an array
– RasenGUY
Nov 22 '18 at 2:25
@NeilLunn , thank you i did not know this part yet, im going to research and find out how to impolment loops so callback s are run serially
– RasenGUY
Nov 22 '18 at 2:25