2

any input would be appreciated.

I'm using an ODBC connection string to connect to my sql database with node.js. I can successfully establish a connection and query the database, but I'm running into trouble when trying to insert data.

The odbc plugin can be found here: https://www.npmjs.com/package/odbc

Here is the example I'm trying to recreate:

    var db = require("odbc")()
  , cn = "DRIVER={FreeTDS};SERVER=host;UID=user;PWD=password;DATABASE=dbname"
  ;

//Blocks until the connection is open 
db.openSync(cn);

db.prepare("insert into hits (col1, col2) VALUES (?, ?)", function (err, stmt) {
  if (err) {
    //could not prepare for some reason 
    console.log(err);
    return db.closeSync();
  }

  //Bind and Execute the statment asynchronously 
  stmt.execute(['something', 42], function (err, result) {
    result.closeSync();

    //Close the connection 
    db.closeSync();
  });
})

Here is my code:

 var db = require("odbc")()
 , cn = "Driver={ODBC Driver 13 for SQL Server};Server=host:insert-name.database.windows.net,insert-port;Database=insert-database-name;Uid=insert-uid;Pwd=insert-password;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;"
;
//Blocks until the connection is open


//Blocks until the connection is open
db.openSync(cn);

//Blocks while preparing the statement


db.prepare("INSERT INTO Contact (FirstName, LastName, Country, User, Email, PrivacyAgreement, PhoneNumber) VALUES (?,?,?,?,?,?,?)", function (err, stmt) {
  if (err) {
    //could not prepare for some reason
    console.log(err);
    return db.closeSync();
  }
  console.log(stmt);
  //Bind and Execute the statment asynchronously
  stmt.execute(['first','last','country_name', 1, '[email protected]', 1, '9999999999'], function (err, result) {
    result.closeSync();
    console.log(result);

    //Close the connection
    db.closeSync();
  });
})

Note: 'User' and 'PrivacyAgreement' are bit datatype (boolean) and the rest are varchar.

To which, I get the following error:

  ODBCStatement { queue: SimpleQueue { fifo: [], executing: true } }
/path/to/file/insert.js:22

 result.closeSync();

    ^

TypeError: Cannot read property 'closeSync' of undefined

2 Answers 2

1

result returned by stmt.execute is undefined, must be because err is thrown, check for error in stmt.execute's callback.

Also is there specific reason why you are using sync operations over asyn? If you have no constrains on making async calls, I would recommend trying https://www.npmjs.com/package/tedious

Sign up to request clarification or add additional context in comments.

Comments

1

Thanks for the input; I did eventually switch over to asyn (using db.openSync to establish the connection and db.prepareSycn).

The issue was arising from the usage of 'User' as a term - 'User' is reserved in the SQL database I'm connecting to. This issue was solved by add brackets to the term 'User' in the statement string.

The following code works:

let stmt = db.prepareSync('insert into Contact (FirstName, LastName, Country, [User], Email, PrivacyAgreement, PhoneNumber) VALUES (?,?,?,?,?,?,?)')

Thank you all!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.