0

我在 Node Js 上使用繁琐。我的代码正在从存储过程中获取输出参数。

我想知道当我在成功的情况下返回解决方案时。

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

以及使用 ofdoneProc的问题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. []
4

1 回答 1

1

像下面那样做,requestCompleted 是你需要把解析值放进去的地方。你得到 3 次 returnValue 的原因是因为你有 3 个输出参数,它在你上面的代码中是正确的。

request.on('requestCompleted',() =>{
     connection.close();        
     done(null, result);  // result is your value want to return   
});
于 2020-07-08T21:04:32.433 回答