0

Hi I have connection with db2 following way which works fine

require("ifxnjs").open(connectionString, function(err, con){  
    If(err){
        console.log(err);
    } else {
        con.query(........){
        }
    }
});

I am trying to create a separate connection factory that can be called in any query. The code I have is

function confactory(){
    db=require("ifxnjs).open(connectionString, function(err, conn){
        if(err){} else {
           console.log('connected');
        }
    })
    return db
}
module.exports=confactory();

Than I access the connection in project

 var db=require('./cofactory.js');
 db.query('select * from emp', function(err, result){
       if(err){}
          else { cons
      }
 })

When I run the program I get error for TypeError Can not read property 'query' of undefined Please let me know how I can fix this issue. Thanks

Kristianmitk
  • 4,528
  • 5
  • 26
  • 46
J. Davidson
  • 3,297
  • 13
  • 54
  • 102
  • 1
    `db=equire` That probably won't work. – CertainPerformance Apr 27 '18 at 08:27
  • `con.query(........)` is "........" a query? – Izio Apr 27 '18 at 08:30
  • The posted code is too fragmentary, and has too many basic typos, for us to help you. Please use copy and paste and then remove the unnecessary bits. Please also ensure the code is formatted and indented in a consistent way so it can be more easily read. – T.J. Crowder Apr 27 '18 at 08:31
  • The problem is that the database connection is created asynchronously, so you cannot get simply get the result and return it from your function. There are many ways to deal with this: callbacks, promises or async/await. Here is the most popular question/answer on Stack Overflow that explains it all: https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Patrick Hund Apr 27 '18 at 08:40
  • You're also missing a quote in `confactory()` after `db=require("ifxnjs`. – Jay Jordan Apr 27 '18 at 18:43

0 Answers0