Currently, I am utilizing the @mean-expert/loopback-sdk-builder to create my API on Angular 4.3.6. However, I encountered an error when executing the following code snippet:
this._userApi.findUsersByRoles([{'name':'cliente'}]).subscribe((users: User[]) => {
this.users = users;
}, (err) => {console.log(err) })
Interestingly, when testing the same method in the LoopBack Explorer, it returns an array of users successfully.
This 'findUsersByRoles' function is auto-generated by the LoopBack SDK:
public findUsersByRoles(roles: any, customHeaders?: Function): Observable<User[]> {
let _method: string = "GET";
let _url: string = LoopBackConfig.getPath() + "/" + LoopBackConfig.getApiVersion() +
"/users/findUserByRoles";
// Rest of the function removed for brevity.
}
The error message displayed in the console can be viewed here.
I suspect that there might be a bug in the SDK builder since my Angular project is relatively new and hasn't undergone major structural changes.
Solution:
After reviewing some feedback, it was pointed out that the issue lies in instances not being recognized as an array. To resolve this, I had to set the property root : true in the remoteMethod definition within the LoopBack model:
returns: {
arg: 'users',
type: 'array',
root : true
}