I have some back-end functions that have been working fine for many months. Recently, I’m starting to see errors, that come in pairs. This doesn’t always happen, it is spurrious:
“[”(node:1) UnhandledPromiseRejectionWarning: TypeError: Cannot read property ‘message’ of undefined\n at DefaultLogger.error (/elementory/node_modules/@wix/cloud-runtime-logger/src/DefaultLogger.ts:52:24)\n at logException (/elementory/node_modules/@wix/cloud-logging/src/timings.ts:42:14)\n at /elementory/node_modules/@wix/cloud-logging/src/timings.ts:55:13"]"
“[”(node:1) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict
(see Command-line API | Node.js v20.5.0 Documentation). (rejection id: 1192)“]”
For the function identified ( getmydeviceinfos ), I am not referencing a ‘message’ property anywhere, and it looks to me like I am properly encapsulating all my await calls with a try…catch, so I am also not clear why I would be getting an unhandledPromiseRejectionWarning exception?
case 'getmydeviceinfos' : {
//console.log("getmydevicesinfo function called.");
response.body = {[RESULT_KEY]:false}; // default false
memberEmail = request.query[EMAIL_ARG];
deviceId = request.query[DEVICEID_ARG];
try {
await isLoginForDeviceValid(deviceId, memberEmail, response);
} catch (error) {
response.body = {[RESULT_KEY]: false,
[ERRORMSG_KEY]:"Invalid website request detected for: " + memberEmail};
return ok(response);
}
if (!response.body.result) {
// Failed to validate caller
response.body = {[RESULT_KEY]: false,
[ERRORMSG_KEY]:"Invalid website request detected for: " + memberEmail};
return ok(response);
}
let connectedMemberDevices = new Array(); // this may end up being empty.
let currentMemberDeviceIDs = null;
let results = null;
try {
// Need the devices for each connection
//console.log("Checking for devices")
results = await wixData.query(SITMEMBERS_TABLE)
.eq(EMAIL_ARG, memberEmail)
.include(MEMBERDEVICES_FIELD)
.find(options);
if (results.items.length > 0) {
//console.log("Found devices for member");
currentMemberDeviceIDs = results.items[0].memberDevices;
} else {
//console.log("Did not find any devices for member with ID: " + connection.connectedMember._id);
currentMemberDeviceIDs = null;
}
if ( currentMemberDeviceIDs !== null ) {
for (var currentDevicesCntr = 0;
currentDevicesCntr < currentMemberDeviceIDs.length;
currentDevicesCntr++) {
connectedMemberDevices.push({[DEVICEID_FIELD]:currentMemberDeviceIDs[currentDevicesCntr].deviceId,
[DEVICEDESCRIPTION_FIELD]:currentMemberDeviceIDs[currentDevicesCntr].deviceDescription,
[FCMTOKEN_FIELD]:currentMemberDeviceIDs[currentDevicesCntr].fcmToken});
}
response.body = {[RESULT_KEY]: true, [MYDEVICEINFOS_KEY] : connectedMemberDevices};
} else {
response.body = {[RESULT_KEY]: false, [ERRORMSG_KEY]:"No devices found for member"};
}
} catch (error) {
response.body = {[RESULT_KEY]: false, [ERRORMSG_KEY]:"Could not get device info for: " + memberEmail};
}
//
return ok(response);
}
The isLoginForDeviceValid() method looks like this:
/*
This function determines whether a login is valid for the given device.
It also checks to make sure login should occur based on other conditions, and
if not, indicates false in the body for result but also with an error message in the body
that identifies the reason for the false result value:
*/
async function isLoginForDeviceValid (deviceId, memberEmail, response, buildNumber) {
// First see if we even know about this member
let deviceToUpdate;
//console.log("isLoginForDeviceValid function called - myDeviceId:" + deviceId + " email: " + memberEmail);
if (typeof deviceId === 'undefined') {
response.body = {[RESULT_KEY]: false, [ERRORMSG_KEY]: USER_ERROR_UNDEFINED};
return "";
}
// Second see if the website is not in "Available" status
//console.log("Checking site availability");
let results;
try {
results = await wixData.query(SITEADMIN_TABLE)
.eq(TITLE_FIELD,SITEADMIN_TITLE_AVAILABILITY)
.find(options);
} catch (error) {
console.log("Got error in query for site admin table.");
response.body = {[RESULT_KEY]: false, [ERRORMSG_KEY]:USER_ERROR_WEBSITE_UNAVAILABLE};
return "";
}
if (results.items.length > 0) {
var item = results.items[0];
if ( item.value !== SITEADMIN_IS_AVAILABLE) {
response.body = {[RESULT_KEY]: false, [ERRORMSG_KEY]:USER_ERROR_WEBSITE_UNAVAILABLE};
return "";
}
} else {
// This should never happen!
//console.log("Could not find the SITEADMIN_AVAILABILITY entry!");
response.body = {[RESULT_KEY]: false, [ERRORMSG_KEY]:USER_ERROR_WEBSITE_UNAVAILABLE};
return "";
}
//console.log("Site available");
//console.log("Gathering device info");
let userid = "";
try {
results = await wixData.query(SITMEMBERS_TABLE)
.eq(EMAIL_ARG, memberEmail)
.include(MEMBERDEVICES_FIELD)
.find(options);
} catch (error) {
console.log("Got error attempting to login user with email: " + memberEmail + " device NOT VALID.");
response.body = {[RESULT_KEY]: false,[ERRORMSG_KEY] : error};
return userid;
}
if (results.items.length === 0 ) {
//console.log("Did not find member with email: " + memberEmail + " device NOT VALID.");
response.body = {[RESULT_KEY]: false, [ERRORMSG_KEY]:USER_ERROR_USER_NOT_FOUND};
return "";
} else {
//console.log("Devices gathered");
// See if the user is suspended
if ( results.items[0].isSuspended) {
//console.log("Found suspended member: " + memberEmail);
response.body = {[RESULT_KEY]: false, [ERRORMSG_KEY]:USER_ERROR_USER_IS_SUSPENDED};
return "";
}
// Find user's associated devices and check them
userid = results.items[0]._id;
let memberDevices = results.items[0].memberDevices;
//console.log("Validating current device against device list.");
let foundCurrentId = false;
let foundDescriptionForId = false;
if ( memberDevices.length > 0) {
for (var i = 0; i < memberDevices.length; i++) {
var device = memberDevices[i];
if (deviceId === device.deviceId) {
foundCurrentId = true;
deviceToUpdate = device;
if ( device.deviceDescription !== "") {
foundDescriptionForId = true;
}
break;
}
}
if ( !foundCurrentId || !foundDescriptionForId) {
response.body = {[RESULT_KEY]: false,[ERRORMSG_KEY] : "You have not registered this device."};
return "";
} else {
response.body.result = true;
// See if we need to update the app build number being used by the user for this device
if (typeof buildNumber !== 'undefined') {
deviceToUpdate.buildNumber = buildNumber;
try {
await wixData.update(MEMBERDEVICES_TABLE, deviceToUpdate, options);
//console.log("Updated device with build number.");
} catch (error) {
//console.log("Error updating build number on device");
}
}
}
} else {
response.body = {[RESULT_KEY]: false,[ERRORMSG_KEY] : "You have not registered this device."};
return "";
}
}
return userid;
}
This looks ok to me also, so what is causing these errors?