【问题标题】:Return the result obtained when using oracledb npm package返回使用 oracledb npm 包时得到的结果
【发布时间】:2017-02-23 18:52:41
【问题描述】:

我在创建 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)) 。我得到未定义的结果。我该如何解决。

【问题讨论】:

    标签: node.js npm oracle11g node-oracledb


    【解决方案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)
      }
    })
    

    【讨论】:

    • 太棒了,我知道这是异步调用的问题,但我不知道如何解决它。谢谢。我会尽可能将其标记为已回答。
    猜你喜欢
    • 2015-05-09
    • 2018-12-26
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 2022-10-04
    相关资源
    最近更新 更多