Start a new topic

unable to find user from collectionAccess.collection('user').find method

Hi,



Im trying to connect my backendpush from delphi XE6 and call my custom endpoints to send push message to another specific user. in this case I'm trying to send message to 'testuser' which already registered to kinvey user table.



fyi, i followed the reference from kinvey BL and ive succeed make broadcast push notification, but when i try to send to one user only, seems the code below didn't work. i can't find the user with 'username':'testuser' and thats why i can't reach the sendPayload method.



below is my code for custom endpoints.



function onRequest(request, response, modules) {



var iOSAps = request.body.iosaps;

var push = modules.push;

var iOSExtras = request.body.iosextras;

var androidPayload = request.body.androidpayload;

var androidmessage = androidPayload.message;

var message = request.body.message;



var collectionAccess = modules.collectionAccess;

collectionAccess.collection('user').findOne({'username': 'testuser')}, function (err, userColl) {

if (err) {

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

} else {

userColl.forEach(function (user) {

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

push.sendPayload(user, iOSAps, iOSExtras, androidPayload);

});

}

});



response.complete( 200 );

}





Need help.

Thanks!

Thanks! I didn't realise before if that response.complete ruined my code!

Thanks guys!
Gal is right.. your code should be structure more like this...



function onRequest(request, response, modules) {

...

var collectionAccess = modules.collectionAccess;

collectionAccess.collection('user').findOne({'username': 'testuser')}, function (err, userColl) {

if (err) {

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

} else {

userColl.forEach(function (user) {

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

push.sendPayload(user, iOSAps, iOSExtras, androidPayload);

});

}

response.complete( 200 );

});



}

Hi, it is likely the problem is that you are calling response.complete() before waiting for the findOne function to complete. The findOne 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 your messages are not being sent 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/
Sorry if this is obvious and you have already tried this. But have you tried to submit the query with the management 'API Console'?



`/user/:appKey/?query={"username":"testuser"}`



Does that fail to return any records as well? If so, you should validate the query is correct and there is in fact data in your user collection that matches what you expect.
Login or Signup to post a comment