Start a new topic

What does this do?

Does the find locate a record in the user table created using the REST POST /user/:appKey/ HTTP/1.1 call?



OR does it locate a record in the Data Store that I have to create and somehow keep in sync with the REST users?



var collectionAccess = modules.collectionAccess;

collectionAccess.collection('user').find({"username": request.body.userid}, function (err, userColl) {

do stuff here;

});

Hi Gary, take a look at this link (from the business logic reference on our devcenter), particularly at the part just below the list of commands, for an example of how to use the asynchronous collectionAccess functions and how to complete endpoint execution only after results are returned http://devcenter.kinvey.com/rest/reference/business-logic/reference.html#collection-access-module
Ok, I'll look at that. I've moaned about not being able to find documentation.



Somebody else said I had to include a response.complete() in the endpoint. In deed I had a case where the endpoint never terminated because it was not there.



I don't recall seeing anything saying the anonymous function was run asynchronously.



Since I need the output before the endpoint terminates, is there a way to run it synchronously?
The problem is that you are calling response.complete() before waiting for the find function to complete. The find function is asynchronous, and will call the callback function you pass to it when it is finished. However, your call to response.complete() happens outside of this callback. The reason you are not seeing the "running function" message is that once response.complete() is called, your endpoint cannot do any more processing.



There are many resources online for learning how to use asynchronous javascript code/callbacks -- one example can be found at http://recurial.com/programming/understanding-callback-functions-in-javascript/
It still doesn't work.



Running Function is never displayed.



And Pushing Message is never displayed.



Request

POST https://baas.kinvey.com/rpc/kid_TT9jfdGrNO/custom/PushUser HTTP/1.1

Authorization: Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

X-Kinvey-API-Version: 3



{

"username": "gwardell",

"message": "Test"

}



Response

HTTP/1.1 200

Content-Type: application/json

X-Kinvey-API-Version: 3

X-Kinvey-Request-Id: a6ba6ec89af44207ad12a299c368a546

X-Powered-By: Express



{}



-----------------------

function onRequest(request, response, modules){

modules.logger.info('Starting');

modules.logger.info('request.body.username=' + request.body.username);

var push = modules.push, collectionAccess = modules.collectionAccess;

collectionAccess.collection('user').find({ "username": request.body.username }, function (err, userColl) {

logger.info('Running Function');

if (err) {

logger.error('Query failed: '+ err);

} else {

userColl.forEach(function (user) {

logger.info('Pushing message to ' + user);

push.sendMessage(user, request.body.message);

});

}

});

modules.logger.info('Ending');

response.complete();

}





Log - Filter Level All



INFO 3:02:45 pm from request a6ba6ec89af44207ad12a299c368a546 in onRequest

"Ending"

INFO 3:02:45 pm from request a6ba6ec89af44207ad12a299c368a546 in onRequest

"request.body.username=gwardell"

INFO 3:02:45 pm from request a6ba6ec89af44207ad12a299c368a546 in onRequest

"Starting"



User Collection



_id username

537979035a2258f7520044a2 dwardell

537980265a2258f75200473f gwardell
Hi Gary, the query above appears to be trying to compare the ID of a user with its username, and since a username is not the same as a user ID, returns no results. You should instead match the correct field. In the case of username, the query could be:



`{ "username": request.body.username }`
Ok, then, if it's the same collection, why does the line I show NOT return any record when there is one record with a matching user name?
Hi Gary, while BL gives you direct access to your data store, without going through the REST API, the underlying 'user' collection is one and the same. Any users created through the REST API will be accessible through BL by querying the 'user' collection. As for push notifications, yes-- they cannot currently be triggered directly through a REST API call, but rather through the appropriate BL function call.
Maybe I should explain that what I'm trying to do is send a push message to specific users.



And from what I understand is that this can't be done from a REST call, and I have to locate the specific user to pass to the sendMessage procedure.
Login or Signup to post a comment