Start a new topic

modules.collectionAccess.collection('user').find(query,callback) stopped working

So I have a collection hook on a collection in which I query the 'user' collection. The query was working for last couple of months but seems to have stopped working now. The callback function doesnt get called at all. Is there some change in the collectionAccess of 'user' collection?

Can you paste in your whole code for the collection hook?
The actual code is a push notification trigger but here is a stripped down code that shows the issue. The callback function never gets called.



Code:

function onPostSave(request, response, modules) {

modules.logger.info("got a save " + JSON.stringify(request.body));

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

modules.logger.info("inside query callback");

if(err) {// Retrieving users failed.

modules.logger.error('Executing posthook: ' + err);

}

else {

modules.logger.info("found some matching users");

}

});

modules.logger.info("outside query callback");

response.continue();

}



Console log:



INFO 10:47:31 PM from request ff6c0e47e35e43e2b8036e5bb262bf0d in onPostSave

"outside query callback"

INFO 10:47:31 PM from request ff6c0e47e35e43e2b8036e5bb262bf0d in onPostSave

"got a save {\"_ccuName\":\"cm rom\",\"_id\":\"84549aa3b0938b58_2000\",\"damper_shape\":0,\"damper_size\":8,\"damper_type\":0,\"floor_name\":\"ff\",\"fsv_address\":2000,\"imei\":\"84549aa3b0938b58\",\"max_damper_open\":97,\"min_damper_open\":40,\"temperature_offset\":-20,\"zone_name\":\"a\",\"_acl\":{\"creator\":\"523be235bb59b95f550054a6\"},\"_kmd\":{\"lmt\":\"2013-12-18T17:17:31.024Z\",\"ect\":\"2013-10-28T06:54:23.361Z\"}}"

Here is the actual push notification definition as well:



Recipents: {"username":"{{request.body.imei}}"}



Message Template: {"username":{{request.body.imei}}, "collection":{{request.collectionName}}, "source":{{request.username}}, "fsv_addr":{{request.body.fsv_address}}}



Generated onPostSave Collection Hook:



function onPostSave(request, response, modules) {

// The code below is auto-generated by the push trigger addon.

// Do not change.

sendPushNotification("{\"username\":{{request.body.imei}}, \"collection\":{{request.collectionName}}, \"source\":{{request.username}}, \"fsv_addr\":{{request.body.fsv_address}}}", "{\"username\":\"{{request.body.imei}}\"}", request, modules);

response.continue();

// End auto-generated code.

}



// The code below is auto-generated by the push trigger addon.

// Do not change.

function sendPushNotification(message, users, request, modules) {

var logger = modules.logger,

utils = modules.utils,

query = utils.renderTemplate(users, { request: request });

modules.collectionAccess.collection('user').find(JSON.parse(query), function (err, userColl) {

if(err) {// Retrieving users failed.

logger.error('Executing posthook: ' + err);

}

else {// Send notification to matching users.

userColl.forEach(function(user) {

var rendered = utils.renderTemplate(

message,

{ user: user, request: request }

);

logger.info('Pushing message: ' + rendered + ' to ' + JSON.stringify(user));

modules.push.send(user, rendered);

});

}

});

}

// End auto-generated code.





This push notification has been working since September and stopped working sometime in the last few days. The query callback function doesnt get called at all.
The issue is that you are executing response.continue() outside the callback. Since find is asynchronous, it starts executing and then program flow continues. Once you execute response.continue, all further business logic callbacks are cancelled.



In the past, this code would sometimes continue unencumbered, but it was never supported. In fact, there would be many instances where the asynchronous code would fail previously in this type of scenario, so whether it succeeded or failed was hit or miss. We recently updated the code to enforce the documented behavior and make callbacks predictable.



To make this script work, place response.continue() immediately following modules.logger.info("found some matching users"); (make sure you also issue a response.error, response.continue, or response.complete in the error condition case of the callback.
HI Michael,



As updated in my last response, this is actually auto generated code in response to a push notification definition. As can be seen the response.continue() is part of the auto-generated code. This would mean that the auto code generation needs to fix this as any code changes that I do manually will get overwritten by any changes in the push notification definition. Let me know if my understanding is incorrect and I should manually edit the generated code?
Please manually edit the generated code. It will not get overwritten unless you make a change to the push trigger. I've submitted a bug report to our console team to ensure that the code generation gets fixed.
Thanks. I fixed the code manually and it is working correctly now.
Login or Signup to post a comment