2016-10-14 3 views
0

oracledb npm package을 사용하여 node.js 백엔드 API를 만들 때 DB와의 연결을 설정합니다. 나는 console.log(result.rows)oracledb npm 패키지를 사용할 때 얻은 결과를 반환하십시오.

아래는 내가 다른 파일에서 getDbConnection 함수를 호출 기능 코드

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

이다 할 때 결과를 얻을 수 있어요 내가 console.log(getDbConnection(query))처럼() CONSOLE.LOG 때. 나는 결과를 정의되지 않은 것으로 얻는다. 어떻게 해결할 수 있습니까?

답변

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) 
    } 
}) 
+0

비상 전화는 비동기 호출의 문제라는 것을 알았지 만 해결 방법을 찾을 수 없었습니다. 고맙습니다. 내가 할 수있을 때 이것을 대답으로 표시 할 것입니다. –

+0

다행입니다. 건배!!! – abdulbarik

관련 문제