-
Notifications
You must be signed in to change notification settings - Fork 177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
findById and find method in custom API - return type problem #63
Comments
Mapping the type of a result while fetching data is an expected behaviour that allows you to extend the SDK Models and add your own logic. Requested in issue #11 it is also documented in here https://github.com/jonathan-casarrubias/loopback-sdk-builder/blob/master/ANGULAR2-DOCS.md#extending-models Since all the methods are created in a loop of methods, fetch methods are added with this functionality and works standarazied -the same- in each of these methods, I may add logic while generating the sdk to avoid doing the check during API calls, but the functionality is expected and will remain. I just don't understand the errors you comment, the functionality has been there since beta 18 and it has been tested in Angular CLI and Web Pack, never had a compilation issue because of this, for instance, it does not really matter if the result is an object or an array, thanks to I don't see how receiving a single object or an array will cause a compilation issue since it is handled in there, both in typings and in javascript logic. But if you got a use case that actually generates compilation issue, could you elaborate and show the compilation error stack? Cheers |
Okay, so you are saying that the code for find() and findById() are coming from same template. The problem I face is the following compilation error (with ng serve / angular-cli)
with following component code
Another error occurs on findById usage which includes message:
To avoid the errors I have to declare my variables as union type
|
So, correct me if I'm wrong, but then you are receiving only 1 school in the call result? its strange because find should always return an array, not sure why would not pass the Array.isArray validation ngOnInit() {
this.service.find()
.subscribe(
schools => this.schools = schools, // <----- HERE?
error => this.errorMessage = <any>error);
} If you are actually receiving an array, then have you tried this? schools: School[];
ngOnInit() {
this.service.find()
.subscribe(
(schools: School[]) => this.schools = schools,
error => this.errorMessage = <any>error);
} |
Of course setting the type in response callback fixes the error. And Yes earlier also the result was in array for find() (thanks to Array.isArray), but wouldnt it be right if the two methods had different result dispensation. |
yes I just wanted to make sure that fixes your problem, but im keeping te issue open to handle find, findOne and findById while generating and not while excecuting |
Fixed, will be launched in RC6 |
In the findById and find methods, the return type is being modified before returning:
findById
find
Shouldnt they be returned as-is ? the find should result in an array always and never as a single Object, which creates problems with the caller, and also results in error on compilation. In case of findById, it can not be an array, so why are we introducing the check.
The text was updated successfully, but these errors were encountered: