I\'m trying to fetch data using mongoose.
So everytime i got to fetch the posts from the api -- localhost:3000/api/posts -- i get the foll error that i am unable to
You need some reject handler for your code, for example:
router.get('/posts', function(req, res) {
console.log('Requesting posts');
post.find({})
.exec()
.then(function(posts) {
res.json(posts);
console.log(posts);
})
.catch(function(error){
console.log('Error getting the posts');
});
});
Or don't use promise chaining use just callback function:
router.get('/posts', function(req, res) {
console.log('Requesting posts');
post.find({}, function(err, posts){
if (err) {
console.log('Error getting the posts');
} else {
res.json(posts);
console.log(posts);
}
})
});