For some reason, my articleExists variable will not set to true on line 6, despite me using console logs to confirm that the if statement that it's contained in is triggering properly.
app.post("/articles", function(req, res) {
let articleExists = (false);
Article.find(function(err, results) {
results.forEach(function(result) {
if (result.title === req.body.title) {
articleExists = (true);
}
});
});
if (articleExists) {
res.send("Article already exists!")
} else {
const newArticle = new Article({
title: req.body.title,
content: req.body.content
});
newArticle.save(function(err) {
if (err) {
res.send(err);
} else {
res.send("Article saved successfuly")
}
});
}
});
trueon line 6?Article.find(..)executed async ?