When trying to log the data from DB using findById
I get a return value of undefined
.
// Get single article
app.get('/article/:id', (req, res) => {
Article.findById(req.params.id, (err, article) => {
console.log(article);
return;
});
});
//Article Schema
let articleSchema = mongoose.Schema({
title: { type: String, required: true },
author: { type: String, required: true},
body: { type: String, required: true}
});
let Article = module.exports = mongoose.model('Article', articleSchema)
i looked up a few things and i got it to work but im not sure if that is the correct way to do it
so what i found was that if i use findOne()
and pass in Article.id
i get the result i want, but is that the correct way of doing things?
i feel like the article.id
should be passed by the req.params.id
, but when i pass that into findOne()
i get an error saying:
Error: Invalid argument to findOne(): 'article_id'