I'm using Tedious on Node Js. My code is getting output paramer from stored procedure.
I'm wonder when I return resolve in success case.
function doInsertDansok(dansokFormBody) {
return new Promise((resolve, reject) => {
connection.on('connect', (err)=> {
if (err) {
log.error('connection error:', err);
reject(consts.JSON_REJECT_500_SQL_ERROR);
return;
}
let output = {o_retKey:null, o_errMsg:null, o_retReceiptInfo:null};
let request = new Request(
'SP_TEST_OUTPUT_PARAM 1, @o_retKey OUT, @o_errMsg OUT, @o_retReceiptInfo OUT',
(err, rowCount, rows)=> {
if (err) {
log.error('request error:', err);
reject(consts.JSON_REJECT_500_SQL_ERROR);
} else {
log.info(rowCount + " rows(s) returned.", rows);
resolve(output); //<== Is this point correct?
}
});
request.addOutputParameter('o_retKey', TYPES.Int);
request.addOutputParameter('o_errMsg', TYPES.VarChar, {length:100});
request.addOutputParameter('o_retReceiptInfo', TYPES.VarChar, {length:255});
request.on('returnValue', (parameterName, value) => {
log.info('request on returnValue: ' + parameterName + ' = ' + value);
output[parameterName] = value; // <-- it called 3 times. so I choose to collecting parameter in here. Is it correct way?
});
request.on('doneProc', () => {
log.info('request doneProc', output);
//resolve(output); //<== Actually I thought this point is the right position to return resolve(). But from log, it was wrong.
});
connection.execSql(request);
});
});
}
And question for usage of doneProc
of request.on
.
Here is the logs.
[2017-12-12T15:21:26.929] [INFO] dansokInsertController - request on returnValue: o_retKey = 2
[2017-12-12T15:21:26.943] [INFO] dansokInsertController - request on returnValue: o_errMsg = Twice
[2017-12-12T15:21:26.943] [INFO] dansokInsertController - request on returnValue: o_retReceiptInfo = sana momomo
[2017-12-12T15:21:26.943] [INFO] dansokInsertController - request doneProc { o_retKey: 2,
o_errMsg: 'Twice',
o_retReceiptInfo: 'sana momomo' }
[2017-12-12T15:21:26.944] [INFO] dansokInsertController - 3 rows(s) returned. []
do it like below way, requestCompleted is where you need to put resolve value in. and the reason you got 3 times returnValue is because you have 3 outputparameters and it is correct in your above code.
request.on('requestCompleted',() =>{
connection.close();
done(null, result); // result is your value want to return
});