I am trying to put a json array in the response of my custom eindpoints.
This is my sample code. It basically receives an array of facebook id's, check them out in the database, then output the username. I am able to get and print the docs.username in the log (so there's result), but when I put it to usernameId[] array when i try to print it, it shows undefined, and the response body is empty.
Can anyone show me what's wrong with the code? Or maybe there's another way to create an array and fill it with the right values?
When a custom endpoint returns an array, you have to use a `KinveyListCallback` instead of a `KinveyClientCallback`. This will give the onSuccess a GenericJson[], which you can use to access all the fields:
endpoints.callEndpoint("getPokupka", gj, new KinveyClientCallback() {
@Override
public void onSuccess(GenericJson result) {
if(result != null) {
Log.d("my", ": " + result.toString());
}
else
Log.d("my", "null");
}
@Override
public void onFailure(Throwable error) {
Log.d("my", "Error " + error);
}
});
The response is equal in the first option: {"aaa": "1", "bbb": "11"}
In the second option result == null.
How to return an array of objects from Custom Endpoints?
Gal
said
over 7 years ago
Hi Dede,
The issue is that findOne is an asynchronous function, which communicates with the database and then, when it's done, calls the callback function. You are calling it in a loop and not waiting for the callback function, which means that your loop completes instantly, and you call response.complete() before any data has actually been retrieved from the database.
Instead, you should construct a more complex query and use the find command (as opposed to findOne) to retrieve all the matching entities from the database. This will be much quicker than executing individual queries for each entity. Then, in the callback, process the resulting array, and call response.complete().
For more information, check the MongoDB documentation (http://mongodb.github.io/node-mongodb-native/api-generated/collection.html#find), as well as the Kinvey Business Logic reference (http://devcenter.kinvey.com/rest/reference/business-logic/reference.html#collection-access-module).
Dede Indrapurna
This is my sample code. It basically receives an array of facebook id's, check them out in the database, then output the username. I am able to get and print the docs.username in the log (so there's result), but when I put it to usernameId[] array when i try to print it, it shows undefined, and the response body is empty.
Can anyone show me what's wrong with the code? Or maybe there's another way to create an array and fill it with the right values?
Thanks,
```function onRequest(request, response, modules){
var logger = modules.logger;
var friendsList = request.body.data;
if(friendsList.length > 0){
var db = modules.collectionAccess;
response.body = '';
//Find facebook Id matches
var usernameId = new Array();
var j = 0;
for(var i=0; i
var facebookId = friendsList[i].facebookId;
db.collection('user').findOne({'_socialIdentity.facebook.id':facebookId}, function(err, docs){
if(err == null){
usernameId[j] = docs.username;
logger.info(docs.username);
j++;
}
});
logger.info("username - " + usernameId[j]);
}
response.body = usernameId;
logger.info(response.body);
return response.complete(200);
}
//No content
else{
return response.complete(204);
}
}```