I'm encountering an issue where the articleExists variable is not being set to true on line 6, even though I have used console logs to double check that the if statement containing it is functioning 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")
}
});
}
});