As of April 12th, you must go to Progress SupportLink to create new support cases or to access existing cases. Please, bookmark the SupportLink URL and use the new portal to contact the support team.
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?
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).
A
Andrey
said
almost 10 years ago
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
Thus the response comes to API Console the correct: array of objects.
On the client of resalt == null
E
Edward
said
almost 10 years ago
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:
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);
}
}```