0

oracledb npm package在创建 node.js 后端 API 时用于建立与数据库的连接。当我这样做时,我能够获得结果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)). 我得到未定义的结果。我该如何解决这个问题。

4

1 回答 1

1

你不能像这样得到你的数据。像这样返回在这里行不通

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

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)
  }
})
于 2016-10-14T11:18:34.580 回答