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.
Found out something really strange. I write following into a preSaveHook
var collectionAccess = modules.collectionAccess;
var myID = collectionAccess.objectID(request.entityId);
var ads = collectionAccess.collection("ads");
var logger = modules.logger;
logger.info(request);
ads.findOne({"_id": myID}....
It all works well if i leave it like this. but as soon as i remove logging info the request.enttityId returns empty string and it breaks everything i trying to do later on. I'm missing something or what?
Best Answer
M
Martin Apostolov
said
over 5 years ago
Hi Jelian,
When removing the logger.info(), you would not be able to inspect the request object and that is why I believe you assume the request.enttityId is returning empty string. Looking at the code snippet, I believe the issue is not in the logger.info() but rather that in the pre-save hook the response.continue() is being called right after the ifclause, which will be executed without waiting for the asynchronous findOne() callback and thus, the
request.body.views = result.views;
will never be executed:
function onPreSave(request, response, modules) {
if (request.method === 'POST') {
if (!request.body.hasOwnProperty('username')) {
request.body.views = 0;
}
} else if (request.method === 'PUT') {
var collectionAccess = modules.collectionAccess;
var myID = collectionAccess.objectID(request.entityId);
var ads = collectionAccess.collection("ads");
var logger = modules.logger;
logger.info(request);
ads.findOne({ "_id": myID }, [], function(err, result) {
if (err) {
response.error(err);
}
// Will never be executed
request.body.views = result.views;
response.continue();
});
}
// This is sinhronous and will be executed right after the "if" clause (will not wait for the findOne asynchronous callback)
response.continue();
}
As the pre-save hook request methods options are only two (POST and PUT), you can change the code like this:
function onPreSave(request, response, modules) {
if (request.method === 'POST') {
if (!request.body.hasOwnProperty('username')) {
request.body.views = 0;
}
// Continue with the exeuction of the request
response.continue();
} else {
var collectionAccess = modules.collectionAccess;
var myID = collectionAccess.objectID(request.entityId);
var ads = collectionAccess.collection("ads");
ads.findOne({ "_id": myID }, [], function(err, result) {
// Will wait for the callback and then continue with the execution of the request
if (err) {
response.error(err);
}
request.body.views = result.views;
response.continue();
});
}
}
Let me know if this has solved the issue.
If the issue persists, could you please describe what error you are receiving?
When removing the logger.info(), you would not be able to inspect the request object and that is why I believe you assume the request.enttityId is returning empty string. Looking at the code snippet, I believe the issue is not in the logger.info() but rather that in the pre-save hook the response.continue() is being called right after the ifclause, which will be executed without waiting for the asynchronous findOne() callback and thus, the
request.body.views = result.views;
will never be executed:
function onPreSave(request, response, modules) {
if (request.method === 'POST') {
if (!request.body.hasOwnProperty('username')) {
request.body.views = 0;
}
} else if (request.method === 'PUT') {
var collectionAccess = modules.collectionAccess;
var myID = collectionAccess.objectID(request.entityId);
var ads = collectionAccess.collection("ads");
var logger = modules.logger;
logger.info(request);
ads.findOne({ "_id": myID }, [], function(err, result) {
if (err) {
response.error(err);
}
// Will never be executed
request.body.views = result.views;
response.continue();
});
}
// This is sinhronous and will be executed right after the "if" clause (will not wait for the findOne asynchronous callback)
response.continue();
}
As the pre-save hook request methods options are only two (POST and PUT), you can change the code like this:
function onPreSave(request, response, modules) {
if (request.method === 'POST') {
if (!request.body.hasOwnProperty('username')) {
request.body.views = 0;
}
// Continue with the exeuction of the request
response.continue();
} else {
var collectionAccess = modules.collectionAccess;
var myID = collectionAccess.objectID(request.entityId);
var ads = collectionAccess.collection("ads");
ads.findOne({ "_id": myID }, [], function(err, result) {
// Will wait for the callback and then continue with the execution of the request
if (err) {
response.error(err);
}
request.body.views = result.views;
response.continue();
});
}
}
Let me know if this has solved the issue.
If the issue persists, could you please describe what error you are receiving?
Regards
Martin Apostolov
1 person likes this
J
Jelian Radoev
said
over 5 years ago
Hi Martin,
Yes what you described was exactly the culprit. It all works as expected now. I have a lot to learn yet, but this was a valuable lesson for sure.
Jelian Radoev
Found out something really strange. I write following into a preSaveHook
var collectionAccess = modules.collectionAccess;
var myID = collectionAccess.objectID(request.entityId);
var ads = collectionAccess.collection("ads");
var logger = modules.logger;
logger.info(request);
ads.findOne({"_id": myID}....
It all works well if i leave it like this. but as soon as i remove logging info the request.enttityId returns empty string and it breaks everything i trying to do later on. I'm missing something or what?
Hi Jelian,
When removing the logger.info(), you would not be able to inspect the request object and that is why I believe you assume the request.enttityId is returning empty string. Looking at the code snippet, I believe the issue is not in the logger.info() but rather that in the pre-save hook the
response.continue()
is being called right after theif
clause, which will be executed without waiting for the asynchronousfindOne()
callback and thus, therequest.body.views = result.views;
will never be executed:
As the pre-save hook request methods options are only two (POST and PUT), you can change the code like this:
Let me know if this has solved the issue.
If the issue persists, could you please describe what error you are receiving?
Regards
Martin Apostolov
- Oldest First
- Popular
- Newest First
Sorted by Oldest FirstMartin Apostolov
Hi Jelian,
I tried followed the steps that you have listed but could not reproduce the described behavior.
Could you please send the whole hook code and your App Key ( kid_............ ) in order to review the server logs?
Regards
Martin Apostolov
Jelian Radoev
Hi Martin,
Here is the hook code https://pastebin.com/yZCpVPD3
AppKey: kid_ByZoedtqf
Martin Apostolov
Hi Jelian,
When removing the logger.info(), you would not be able to inspect the request object and that is why I believe you assume the request.enttityId is returning empty string. Looking at the code snippet, I believe the issue is not in the logger.info() but rather that in the pre-save hook the
response.continue()
is being called right after theif
clause, which will be executed without waiting for the asynchronousfindOne()
callback and thus, therequest.body.views = result.views;
will never be executed:
As the pre-save hook request methods options are only two (POST and PUT), you can change the code like this:
Let me know if this has solved the issue.
If the issue persists, could you please describe what error you are receiving?
Regards
Martin Apostolov
1 person likes this
Jelian Radoev
Hi Martin,
Yes what you described was exactly the culprit. It all works as expected now. I have a lot to learn yet, but this was a valuable lesson for sure.
Thanks a lot for helping me!
Regads
Jelian Radoev
-
How do I access query string values for GET and DELETE requests?
-
Basic Authentication in Business Logic
-
How do I cascade delete entities from related collections?
-
All BL failing with Violation Error
-
How do I send push notifications?
-
Whenever I try to query by _id, I get zero results
-
receiving SOCKETTIMEDOUT when requesting external http response
-
Unique Constraints on a collection column?
-
Need some help with grouping
-
Accessing Endpoint from Collection Hook
See all 342 topics