-8

I am new to loopback and I have a problem with it. I have created and managed a Postgres database and everything seems to work fine. The database is created by boot files in json and contains the needed values.

But when I want to execute findById method, I get the following issue :

Error for the request GET /api/members/1?id=1 : Error: Model::findById require the id argument

the model definition is :

{
    "name": "Member",
    "strict": true,
    "idInjection": true,
    "base": "User",
    "options": {
       "validateUpsert": true
    },
    "properties": {
    "firstname": {
    "type": "string",
    "required": true
    },
    "lastname": {
    "type": "string",
    "required": true
    },
    "questiontype": {
    "type": "number",
    "required": true
    },
    "questionanswer": {
    "type": "string",
    "required": true
    }
    },
    "validations": [],
    "relations": {
    "issues": {
       "type": "hasMany",
       "model": "Issue",
       "foreignKey": "authorId"
    }

And I call findById from my LoginActivity :

 connect_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RestAdapter restAdapter = new RestAdapter(getApplicationContext(), "http://" + MY_IP + ":3000/api");
            MemberRepository memberRepo = restAdapter.createRepository(MemberRepository.class);
            memberRepo.findById(1, new ObjectCallback<Member>() {
                @Override
                public void onSuccess(Member member) {
                    System.out.println("Found !");
                }

                public void onError(Throwable t) {
                    System.out.println("Fail");
                }
            });
        }
    });

The thing is that when I execute findById method on localhost:3000/explorer from loopback, it returns the right result and no error at all.

The problem only appears when on Android. I use the strongloop library provided by loopback which use a UserRepository class that contains login, logout, find,.. methods. So they should not be any error coming from the method itself. Which lead me to the actual problem, I really can't figure out where it is from ?

  • Please also translate the error, and include it as text – Rob May 08 '18 at 11:44
  • 1
    Ok it's done now – Laura Lopez May 08 '18 at 18:24
  • To check if the issue is Android related try to access the webservice's url (should be `http://" + MY_IP + ":3000/api/members/1?id=1`) directly from your browser. Do you get any result? – LS_ May 09 '18 at 10:22
  • Hi, yes when I do that directly from the browser it returns a json of the member with id = 1 – Laura Lopez May 09 '18 at 10:35
  • I'm trying to guess, but if you can see the results by directly accessing the url it's possible that in Android the code generates a `POST` request, while only `GET` might be enabled. Do you have access to the method that exposes the webservice? If yes, can you add the code to the question? – LS_ May 09 '18 at 10:51
  • Yes the method used is in this file : [Strongloop github](https://github.com/strongloop-community/loopback-sdk-android/blob/master/src/main/java/com/strongloop/android/loopback/ModelRepository.java) – Laura Lopez May 09 '18 at 11:00
  • @LauraLopez I took a look at the class, can you try to change the line: `MemberRepository memberRepo = restAdapter.createRepository(MemberRepository.class);` with `MemberRepository memberRepo = restAdapter.createRepository(MemberRepository.class, "Member", Member);` and check if you get the same response? – LS_ May 09 '18 at 14:04
  • I just tried and unfortunately there is still the same error – Laura Lopez May 09 '18 at 19:05

2 Answers2

1

So after a few days of trying everything, I just gave up on strongloop and tried using Retrofit2. Turns out everything works perfectly now.

Thanks again @Signo for you time! Quite a frustrating ending, but I think as you said, it was probably not provided the right request and I guess the fact that it was not mantained anymore just finished me off.

  • Probably the best choice, I think `Retrofit` and `Gson` are the best libraries for handling webservices. (This helpful [tool](http://www.jsonschema2pojo.org/) saved me a lot of time when I had to create the models) Happy coding :) – LS_ May 13 '18 at 19:17
0

I'll try to give some suggestions, let me know if any of this work so I can edit the answer:

  • From your error: /api/members/1?id=1 is it correct that it uses members instead of member in the path? members comes from this line English.plural(className); ;
  • If you have access to the Strongloop library try to add a breakpoint in the return statement of getVerbForMethod in RestContract here and check if your call returns GET;
  • There's an answer here for your same error but generated from a js call where they suggest to edit your object's remoteMethod;

ps: from the Strongloop website it states that the Android library is not being actively mantained anymore

LS_
  • 6,763
  • 9
  • 52
  • 88