2

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'
user8259536
  • 23
  • 2
  • 7
  • 1
    Try logging out the `err` parameter – Baruch Aug 27 '17 at 16:08
  • this is what i get: CastError: Cast to ObjectId failed for value "article_id" at path "_id" for model "Article – user8259536 Aug 27 '17 at 16:53
  • the current error i'm facing is when i try to log an article value i get an undefined value with the error : `message: 'Cast to ObjectId failed for value "article_id" at path "_id" for model "Article"'` i did face a problem with having misnamed the params but that has been fixed i can get it to show articles using `Articles.find()` but when trying to use `Article.findById` problems being to happen – user8259536 Aug 29 '17 at 07:36
  • this is done the mistake was at the index html i made a mistake in passing in the params and passed them in as a string which is why it couldn't read the id, i found this when i inspected the index file. well on to the next thing – user8259536 Aug 29 '17 at 07:57

2 Answers2

1

Use Article.findById(mongoose.Types.ObjectId(req.params.id)}, (err, article)...

As you are looking for object id and not a String.

HatzavW
  • 560
  • 1
  • 4
  • 16
  • i did that and i got the following: `Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters ` so i went and checked the database because i suspected that there maybe no value stored for id which is why it wouldn't show me it, but when i did check for id it was there so im back to square one agaiin – user8259536 Aug 29 '17 at 06:15
  • Could it be that `req.params.id` not exist? – HatzavW Aug 29 '17 at 06:22
  • well i can log `req.params.id ` and it returns the correct value – user8259536 Aug 29 '17 at 06:45
  • ok my bad i found that i loaded in the wrong params which is why it couldn't find the id, i gave it the param of `article_id` which should have been `article._id` – user8259536 Aug 29 '17 at 06:48
  • so i could get the values using `Article.find` but when i tried to use `Article.findById` i got an undefined value again O.o" – user8259536 Aug 29 '17 at 06:53
  • "findById is a convenience method on the model that's provided by Mongoose to find a document by its _id." from- https://stackoverflow.com/questions/12483632/mongodb-via-mongoose-js-what-is-findbyid – HatzavW Aug 29 '17 at 07:00
  • yeah i had a look at that a while ago, i tried doing this `.findOne({_id: req.params.id}` to check if i could get it to show the article but no luck i also tried using `Article.findOne()` that returns undefined as well – user8259536 Aug 29 '17 at 07:29
0

Are you pulling in the correct ID from your database, on your link that sends you to /article/:id. I had the same problem, as I previously learned on basic array indexes and moved to MongoDB. I just needed to update one character to include the underscore for the MongoDB syntax of '_id' as opposed to 'id'.

The correct code that worked for me on my HTML rendered pages had the below:

<a href={`/article/${article._id}`}>

What I started with and gave me an undefined result was the below:

<a href={`/article/${article.id}`}>