Start a new topic

Creating Array in Custom Endpoints

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?



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);

}

}```

certainly

thanks
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:



AsyncCustomEndpoints endpoints = mKinveyClient.customEndpoints();

GenericJson gj = new GenericJson();

endpoints.callEndpoint("getPokupka", gj, new KinveyListCallback() {

@Override

public void onSuccess(GenericJson[] result) {

if(result != null) {

Log.d("my", ": " + result[0].toString());

}

else

Log.d("my", "null");

}



@Override

public void onFailure(Throwable error) {

Log.d("my", "Error " + error);

}

});
Above the simplified example is given.

Here a living example which I faced:



modules.collectionAccess.collection('Spisok').find({"_acl.creator": user_id, "dt": request.body.dt}, function(err, docs) {

if(err == null && docs != null) {

response.body = docs;

response.complete(200);

}



Thus the response comes to API Console the correct: array of objects.

On the client of resalt == null
Such feeling that answered other question. There is neither find, nor findone.

It is a question that it is necessary to return to Custom Endpoints not object, but the array of objects



1. Correctly such code works:



function onRequest(request, response, modules){

response.body = {"aaa": "1", "bbb": "11"};

response.complete(200);

}



2. And such returns empty value:



function onRequest(request, response, modules){

response.body = [ {"aaa": "1", "bbb": "11"}.

{"aaa": "2", "bbb": "22"}

];

response.complete(200);

}



Data I obtain so:



AsyncCustomEndpoints endpoints = mKinveyClient.customEndpoints();

GenericJson gj = new GenericJson();

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?
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).
Login or Signup to post a comment