Currently, I have set up a Nestjs rest server that includes a controller and a service.
Within the controller, there is a get function that is triggered when a get request is made:
@Get()
getAllFoos() {
return this.fooService.getAllFoos();
}
Inside the service, there exists a function responsible for fetching documents from a database:
async getAllFoos(): Promise<foos[]> {
try {
return await this.fooModel.find().exec();
} catch(e) {
return e;
}
At this point, the setup is functional and operational. However, I now aim to modify the system to operate using observables instead.
For the controller, I made the following changes:
@Get()
getAllFoos() {
this.fooService.getAllFoos().subscribe(
response => {
console.log(response);
},
error => {
console.log(error);
},
() => {
console.log('completed');
});
}
And with the service, the adjustments were as follows:
getAllFoos(): Observable<foos[]> {
try {
this.fooModel.find().exec();
} catch(e) {
return e;
}
}
Upon testing this new setup, an error message is generated:
[Nest] 7120 - 2019-2-20 15:29:51 [ExceptionsHandler] Cannot read property 'subscribe' of undefined +4126ms
This error specifically relates to the following line within the controller:
this.fooService.getAllFoos().subscribe(
Unfortunately, I am currently unsure about the necessary changes to resolve this issue. Any assistance or insight would be greatly appreciated!