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).
over 5 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
over 5 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: