1

I'm developing an Android application in conjunction with Strongloop/Loopback. I've stored my data in a MySQL database and have no problem mapping this with Strongloop/Loopback. However, when retrieving the values from the database using Strongloop/Loopback, the list always return a size but with null values. Can't figure out what is wrong. Can anybody help me with this? Many thanks :)

Here is my json response for the database when access from Strongloop:

[
  {
"rewards_image_name": "http://x.x.x.x/projects/images/rewards_1.png",
"rewards_equivalent_points": "10",
"id": 1 }, {
"rewards_image_name": "http://x.x.x.x/projects/images/rewards_2.png",
"rewards_equivalent_points": "20",
"id": 2 }, {
"rewards_image_name": "http://x.x.x.x/projects/images/rewards_3.png",
"rewards_equivalent_points": "30",
"id": 3 }, {
"rewards_image_name": "http://x.x.x.x/projects/images/rewards_4.png",
"rewards_equivalent_points": "40",
"id": 4 }
]

Here is my code for getting the list:

    StrongloopClient strongloopClient = new StrongloopClient(this.context);
    RestAdapter adapter = strongloopClient.getLoopBackAdapter("Rewards", "GET");
    StrongloopRewardsModelRepository strongloopRewardsModelRepository = adapter.createRepository(StrongloopRewardsModelRepository.class);       
    strongloopRewardsModelRepository.findAll(new ListCallback<StrongloopRewardsModel>() {

        @Override
        public void onSuccess(List<StrongloopRewardsModel> arg0) {
            Log.e("", "GetAllRewards success:  " + arg0.get(0).getEquivalentPoints());

        }

        @Override
        public void onError(Throwable arg0) {
            Log.e("", "GetAllRewards error: " + arg0);
        }
    });

Here is the StrongloopClient:

public class StrongloopClient {

private Context context;
private RestAdapter adapter;

public StrongloopClient(Context contxt) {
    context = contxt;
}

    public RestAdapter getLoopBackAdapter(String transaction, String operation) {
    if (adapter == null) {
        adapter = new RestAdapter(context, "http://192.168.44.101:3000/api");
        adapter.getContract().addItem(
                new RestContractItem("/" + transaction, operation),
                transaction);
    }
    return adapter;
}   

Here is the code for Repository:

public class StrongloopRewardsModelRepository extends ModelRepository<StrongloopRewardsModel>{

public StrongloopRewardsModelRepository() {
    super("Rewards", "Rewards", StrongloopRewardsModel.class);  
    }
}

And this is the Model:

public class StrongloopRewardsModel extends Model {

private Integer rewardsImageId;
private String rewardImageName;
private String equivalentPoints;

public Integer getRewardsImageId() {
    return rewardsImageId;
}
public void setRewardsImageId(Integer rewardsImageId) {
    this.rewardsImageId = rewardsImageId;
}
public String getRewardImageName() {
    return rewardImageName;
}
public void setRewardImageName(String rewardImageName) {
    this.rewardImageName = rewardImageName;
}
public String getEquivalentPoints() {
    return equivalentPoints;
}
public void setEquivalentPoints(String equivalentPoints) {
    this.equivalentPoints = equivalentPoints;
}
}
Master_A2Z
  • 97
  • 2
  • 12
  • Please post the source of StrongloopRewardsModel and StrongloopRewardsModelRepository. The call "adapter.getContract().addItem(...)" is redundant in this example. – Miroslav Bajtoš Jun 17 '14 at 06:52
  • @MiroslavBajtoš, updated my question to include source code for Model and Repository – Master_A2Z Jun 17 '14 at 07:56

2 Answers2

1

Finally, I've found what was wrong. POJOs should match the fields of models created in models.json. Thanks for your time.

However, my other question is that, when I used filters for querying such as passing the filters as part of "parameters" being passed to Strongloop adapter, it seems to return all of the model instances instead of filtered ones. Same code from my question, just that getLoopbackAdapter("Rewards","GET") becomes "Rewards?filter[where][rewards_equivalent_points]=10","GET"). Any ideas why it behaved like that? Thanks :)

Master_A2Z
  • 97
  • 2
  • 12
0

This is because your search didn't find any instance. I'm not sure if you need to handle this on your App or if the SDK deals with that.

For example, when trying to search by an element[1] through the rest interface, it gets converted to 404 (not found): rest: {after: convertNullToNotFoundError}

https://github.com/strongloop/loopback-datasource-juggler/blob/master/lib/dao.js#L771

  • actually the SDK returns ALL of the instances when using the modelrepository.findAll feature even though a "filter" was applied on the url being passed on the adapter, this is what i don't understand. However, using apache httpclient returns the filtered model instances. Any advice? – Master_A2Z Jun 21 '14 at 09:33