1

I am working on a express app with mongoose.

the .find method is working in other route but inside my blogs route, it is not working.

Here is my mongoose model:

    const mongoose = require('mongoose');

let BlogSchema = new mongoose.Schema({
    title: {type: String, unique: true, required: true},
    body: {type: String},
    category:  String,
    permalink: String,
    date: {type: Date, default: Date.now},
    image: {type: String},
    imageId: String,
    tags:[{type: String}],
    author: {
        id: {
            type: mongoose.Schema.Types.ObjectId,
            ref: "User"
        },
        username: String
    }
});


  let Blog = mongoose.model('Blog', BlogSchema);

  module.exports = Blog;

and here is my router.get :

//SHOW ROUTE
router.get('/blog/:category/:permalink', (req,res)=>{

        console.log(req.params) //{ category: 'travel', permalink: 'why-travel-to-places' }
    Blog.find({category: req.params.category,permalink: req.params.permalink}, (err, foundPost)=> {
        if (err){
            console.log(err)
        } else {
            console.log(foundPost) // []
            res.render('blog/show', {post: foundPost});
        }
    });
});

As you can see, I console.log req.params and foundPost . req.params result is { category: 'travel', permalink: 'why-travel-to-el-nido' } and foundPost is an empty array []

Now I looked at mongo using db.blogs.find({category: "travel"}).pretty() and it found it. But inside my show route ,it is not finding it?

I looked up on other questions related to my problem and someone said that perhaps it has a problem with schema, but in my case, it's not.

my .find() is working in in other routes but in it's not working in this route.

Did I miss something, please help. Thank you

2
  • 2
    Are you sure you are pointing at the correct database namespace, or even server? You are looking correctly for the collection name of blogs because that is what mongoose is doing, but make sure your application connection string is pointing where you think it is. Remembering when you open a mongo shell, the database is test. Also turn on debugging. mongoose.set('debug', true)
    – Neil Lunn
    Commented Mar 4, 2019 at 9:00
  • Thank you, It turns out. in the MongoDB. my permalink has ? inside the permalink. But inside mongoose. there is no ? . Thank you very much
    – bradrar
    Commented Mar 4, 2019 at 13:54

1 Answer 1

1

I'm pretty sure that the .find() is working correctly, but is not finding any document in the database with that query criteria. Note that in the route code you are querying { category: 'travel', permalink: 'why-travel-to-el-nido' }, i.e that the matched Blog document must have the fields category: 'travel' AND permalink: 'why-travel-to-el-nido', but when you look at Mongo using db.blogs.find({category: "travel"}).pretty() you are only looking for Blog documents that have {category: "travel"}, but no restriction for the permalink field.

Please, search again in Mongo using db.blogs.find({category: "travel", permalink: "why-travel-to-el-nido"}).pretty() to simulate the same query from the route, and see if you can find any document that matches that criteria.

My guess is that you won't find any, so the mongoose .find() is returning an empty array, meaning "no matches".

Edit: I just saw the comment from Neil Lunn, and it might be possible that the mongoose configuration is not pointing to the right database. Follow his instructions to make sure you are querying to the collection you want.

2
  • You're right, I am not finding anything with db.blogs.find({category: "travel", permalink: "why-travel-to-el-nido"}).pretty(). Thank you for pointing that out.
    – bradrar
    Commented Mar 4, 2019 at 13:41
  • Thank you, It turns out. in the MongoDB, my permalink has ? inside the permalink. But inside mongoose. there is no ? . Thank you very much. It's working now
    – bradrar
    Commented Mar 4, 2019 at 13:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.