何时在调用存储过程后返回resolve()

时间:2017-12-12 23:40:58

标签: node.js tedious

我在Node Js上使用Tedious。 我的代码从存储过程中获取输出paramer。

我很想知道我在成功的情况下何时回归决心。

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);
    });
  });
}

使用doneProc request.on的问题。

这是日志。

[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. []

1 个答案:

答案 0 :(得分:1)

按以下方式进行操作,requestCompleted是您需要放置解析值的位置。获得3倍returnValue的原因是因为您有3个outputparameters,并且在上面的代码中是正确的。

request.on('requestCompleted',() =>{
     connection.close();        
     done(null, result);  // result is your value want to return   
});