DEV Community

sm-a
sm-a

Posted on

2

[SOLVED] Mongoose create not executed when I do before a for loop. Please help?

When I execute the code below I don't get any documents in Mongoose for "Detail"-collection - see line 11 in the code. Why doesn't it work? I am grateful for every tip.

Cats.find({ user: req.params.user }, function (err, product) {
if (err) {
return res.send({ isFound: false });
}
if (!product) {
return res.send({ isFound: false });
}
product.forEach(element => {
element.orderid = req.params.smaorderid
Detail.create(element, function (err, orderitems) {
if (err) {
console.log(err)
return res.send({isCreated: false})
}
})
});
return res.send({isCreated: true})
})

I made a mistake in the previous code. Therefore it is solved now.

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

The only thing worse than downtime? No Answers.

If you’re sometimes frustrated with opaque infrastructure, sluggish support, and mysterious outages, we prepared a webinar just for you

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️