I'm encountering an issue when trying to send a mongodb collection from my server to the client. The error message Cannot read property then of undefined is showing up in the server-side controller. It seems like there is an issue with the promise, but I'm unable to figure it out. What changes need to be made in the code to resolve this issue? Please let me know if you require additional code snippets.
The problem arises at ".then(....)" within the below function.
course.controller.js
function getAll(req,res){
CourseService.getAll()
.then(function(result){
if(result){
res.send(result);
}else{
res.sendStatus(404);
}
})
.catch(function(err){
res.status(400).send(err);
});
}
The getAll function in CourseService looks like this:
course.service.js
function getAll(){
console.log('services/course.service getALL');
var deferred = Q.defer();
db.collection('courses').find().toArray(function(err, result) {
if (err) deferred.reject(err);
console.log(result);
deferred.resolve();
return deferred.promise;
});
}