Reputation: 342
I am trying to get posts of my friends like social networking. Since, I cannot use the things like "INNER JOIN" in client side with Easy tables, I have to use custom api. Here is my approach:
Android client:
List<Pair<String, String>> params = new ArrayList<Pair<String, String>>();
params.add(new Pair("id", user.getId()));
params.add(new Pair("otherParam", otherParam));
client.getClient().invokeApi("getFeed", "POST", params, new ApiJsonOperationCallback() {
@Override
public void onCompleted(JsonElement jsonElement, Exception exception, ServiceFilterResponse response) {
System.out.println("exception: " + exception.toString());
GsonBuilder gsonb = new GsonBuilder();
Gson gson = gsonb.create();
JsonArray array = jsonElement.getAsJsonArray();
for (int i = 0; i < array.size(); i++) {
postList.add(gson.fromJson(array.get(i).getAsJsonObject().toString(), Post.class));
}
}
});
feed.js:
var api = {
post: (request, response, next) => {
var query = {
sql: 'SELECT * FROM Posts INNER JOIN Friends ON Posts.userId = Friends.followingId WHERE Friends.followingId = @id ORDER BY Posts.createdAt DESC',
parameters: [
{ id: 'id', otherParam: 'otherParam' }
]
};
request.azureMobile.data.execute(query)
.then(function (results) {
response.json(results);
});
}
};
module.exports = api;
I am not even sure whether this is a correct way to do it or we can access tables in easy tables like this, but there is not any detail in exception print. Exception is "Error while processing request". Thanks in advance.
Upvotes: 3
Views: 135
Reputation: 1701
Your code is very close - it is just the specification of parameters to the SQL statement that is not quite right. You need to specify parameters in the following format:
var query = {
sql: 'SELECT * FROM Posts INNER JOIN Friends ON Posts.userId = Friends.followingId WHERE Friends.followingId = @id ORDER BY Posts.createdAt DESC',
parameters: [
{ name: 'id', value: 'id' },
{ name: 'otherParam', value: 'otherParam' }
]
};
API reference for this is at http://azure.github.io/azure-mobile-apps-node/module-azure-mobile-apps_src_data_execute.html.
To get a bit more debugging information out of the server, you can bump up the logging level. If you are running locally, run the app with an additional command line parameter:
node --debug app.js ---logging.level silly
If you are running hosted on Azure, you can set the log level for Application Logging (filesystem) to verbose and open the log stream. This will show you the exact SQL statements being executed and the parameters.
Upvotes: 2