Return使用oracledb npm包时得到的结果

Return the result obtained when using oracledb npm package

我在创建 node.js 后端 API 时使用 oracledb npm package 与数据库建立连接。当我做 console.log(result.rows)

时,我能够获得结果

下面是功能代码

      getDbConnection: function(query) {

            var connectInfo= [EnvConfig.getConnectionHostName(), EnvConfig.getConnectionPort()];   // Array of hostname and port
            var connectHost = connectInfo.join(':');  // join hostname and port with ':'
            var connectionAttr = [connectHost, EnvConfig.getConnectionSID()]; // hostname:port and SID
            var connectString = connectionAttr.join('/'); // creating the connection string like 'hostname:port'
            console.log(connectString);

            // creating a oracle connection
            oracledb.getConnection(
                {
                    user: EnvConfig.getConnectionUserName(),
                    password: EnvConfig.getConnectionPassword(),
                    connectString: connectString
                },
                function (err, connection) {
                    if(err) {
                        console.log(err.message);
                        return;
                    }
                    connection.execute(
                        query, function(err, result) {
                            if(err) {
                                console.log(err.message);
                                return;
                            }
                            console.log(result.rows);   // I get result here
                            return result.rows;   // return result on success
                        }
                    );
                }
            );          
        }

我从其他文件调用 getDbConnection 函数,当我 console.log() 喜欢 console.log(getDbConnection(query)) 时。我得到的结果是未定义的。我该如何解决这个问题。

you can't get your data like this. Return like this will not gonna work here

由于它以异步方式工作,您可以做一件事来使用回调函数来获得这样的结果

DbConnection: function(query,callback) {//pass callback as parameter 

  var connectInfo = [EnvConfig.getConnectionHostName(), EnvConfig.getConnectionPort()]; // Array of hostname and port
  var connectHost = connectInfo.join(':'); // join hostname and port with ':'
  var connectionAttr = [connectHost, EnvConfig.getConnectionSID()]; // hostname:port and SID
  var connectString = connectionAttr.join('/'); // creating the connection string like 'hostname:port'
  console.log(connectString);

  // creating a oracle connection
  oracledb.getConnection({
      user: EnvConfig.getConnectionUserName(),
      password: EnvConfig.getConnectionPassword(),
      connectString: connectString
    },
    function(err, connection) {
      if (err) {
        console.log(err.message);
        return callback(err);
      }
      connection.execute(
        query,
        function(err, result) {
          if (err) {
            console.log(err.message);
            return callback(err);
          }
          console.log(result.rows); // I get result here
          return callback(null,result.rows); // return result on success
        }
      );
    }
  );
}

然后这样调用

//Use your function with callback
getDbConnection(query,function(err,result){
  if(!err){
    console.log(result)
  }
})