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
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 amongo
shell, the database istest
. Also turn on debugging.mongoose.set('debug', true)
?
inside the permalink. But inside mongoose. there is no?
. Thank you very much