Details
-
Bug
-
Status: Open (View Workflow)
-
Major
-
Resolution: Unresolved
-
None
-
None
-
None
-
None
-
node => v16.13.1
"express": "^4.17.2",
"joi": "^17.6.0",
"mariadb": "^2.5.5",
Description
We use stored procedures for everything. We also use sequences and want to return the sequence number back from the stored procedure as an out parameter. We still haven't gotten to select statements yet in our prototype work, but that is beginning to scare me too. I am new to JavaScript, so it could be me making a newbie mistake with promises. Here is my route.
authProcRoutes.post('/userinvite', async (req, res, next) => { |
let conn;
|
let emailSent = false; |
|
console.log(req.body);
|
|
try { |
// validate input |
const inputValidated = await validateUserInviteInput(req.body); |
if (typeof inputValidated === 'string' || inputValidated instanceof String) { |
return res.status(400).send((inputValidated)); |
}
|
|
// process security settings |
const exprDateTimeUTC = new Date(getDatePlus(48).toUTCString()); |
const userPWResetExpr = exprDateTimeUTC.toISOString().replace('Z','').replace('T', ' '); |
const userPWResetToken = await hashPassword(userPWResetExpr + req.body.userEmail); |
|
// connect to database |
const mdbpool = getPool(); |
conn = await mdbpool.getConnection();
|
console.log("connected ! connection id is " + conn.threadId); |
|
const dbResponse = await conn.query( |
'CALL auth_user_P__userinvite_sp(?,?,?,?,?,?,?,?,?,@NewRcrdID); SELECT @NewRcrdID;', [ |
req.body.customerID,
|
req.body.userEmail,
|
req.body.userFirstName,
|
req.body.userLastName,
|
userPWResetToken,
|
userPWResetExpr,
|
req.body.userType,
|
req.body.userSummary,
|
req.body.userRequesterID
|
]
|
);
|
|
|
console.log(dbResponse);
|
const dbrSQL1 = dbResponse[0]; |
const dbrOkPacket = dbResponse[1]; |
const dbrSQL2 = dbResponse[2]; |
const resObject = dbrSQL2[0]; |
console.log(dbrSQL1);
|
console.log(dbrOkPacket);
|
console.log(dbrSQL2);
|
console.log(resObject['@NewRcrdID']); |
|
if (dbResponse[1].affectedRows > 1) { |
// emailSent = code to send email |
|
};
|
|
if (conn) conn.end(); |
console.log('conn.end()'); |
res.send('Invite sent to ' + req.body.userFirstName); |
} catch (err) { |
if (conn) conn.end(); |
console.error("Catch Block: ", err); |
res.status(500).send('Error: ' + err.text); |
}
|
});
|
And I get this as my result from my console.log deconstruction of the dbResponse object:
There has to be a better way to get to the data vs what I have in my function. I can't imagine doing this kind of stuff for 100 rows of data vs one out param.
[
[
, meta: [ [ColumnDef] ] ],
OkPacket
,
[
, meta: [ [ColumnDef] ] ]
]
[
,
meta: [
ColumnDef
]
]
OkPacket
[
,
meta: [
ColumnDef
]
]
10014
I am new to JavaScript and I may be missing some syntatical sugar, but this is what I came up with as a work around:
message: dbResponseError
};
I am hoping I am missing something or you will add something to the system to make this information more accessible for multi-statement queries without jumping through hoops.