I'm currently facing an issue with my code. I have a ProductService
which includes a boolean function called validateProductsBeforeChanges
.
Within the validateProductsBeforeChanges
function, I am calling another service named OrderService
, which returns an observable.
Here is an example of the code:
validateProductsBeforeChanges(idProduct): Boolean {
this._orderService.getAll()
.subscribe(orders => {
this.orders = orders;
this.ordersWithCurrentMenu = this.orders.filter(x =>
x.products.find(y => y.code == idProduct));
if(this.ordersWithCurrentMenu.length > 0) {
return false;
}
else {
return true;
}
});
}
The issue arises when vs code throws a syntax error:
A function whose declared type is neither 'void' nor 'any' must return a value
To resolve this, I attempted the following approach:
validateProductsBeforeChanges(idProduct): Boolean {
this._orderService.getAll()
.subscribe(orders => {
this.orders = orders;
this.ordersWithCurrentMenu = this.orders.filter(x => x.products.find(y => y.code == idProduct));
if (this.ordersWithCurrentMenu.length > 0) {
return false;
}
else {
return true;
}
});
return false;
}
However, in this scenario, the final return statement is executed before the .subscribe
completes, causing the function to always return false.
Do you have any suggestions on how to address this issue?
Thank you for your assistance!