Unable to connect remote db2 with node js - node.js

I am trying to connect remote ibm db2 with node js in mac machine but I'm getting following error
[Error: [IBM][CLI Driver] SQL1598N An attempt to connect to the database server failed because of a licensing problem. SQLSTATE=42968
] {
error: '[node-ibm_db] SQL_ERROR',
sqlcode: -1598,
message: '[IBM][CLI Driver] SQL1598N An attempt to connect to the database server failed because of a licensing problem. SQLSTATE=42968\n',
state: '42968'
}
And this is how I'm trying to connect
function db2Connection() {
var ibmdb = require('ibm_db');
var connStr = "DATABASE=dbname;HOSTNAME=mydb.ibm.com;UID=userid;PWD=password;PORT=447;PROTOCOL=TCPIP";
ibmdb.open(connStr, function (err,conn) {
if (err) return console.log(err);
conn.query('SELECT * FROM T_Name FETCH FIRST 6 ROWS ONLY', function (err, data) {
if (err) console.log(err);
else console.log(data);
conn.close(function () {
console.log('done');
});
});
});
}
db2Connection();
I went through some docs regarding that error but did not get any solution. Can anyone help on how to achieve this.

Related

Nodejs Snowflake connection - Error [NetworkError]: Network error. Could not reach Snowflake

I am trying to connect snowflake, using the node.js driver found in the snowflake documentation
var snowflake = require('snowflake-sdk');
var connection = snowflake.createConnection({
account: 'account1',
username: 'user1',
password: 'pass1',
region: 'us-east-1'
});
connection.connect(function(err, conn) {
if (err) {
console.error('Unable to connect: ' + err.message);
} else {
console.log('Successfully connected as id: ' + connection.getId());
}
});
Error: Network error. Could not reach Snowflake.
code: 401001,
cause: Error: unable to get local issuer certificate

"Failed to lookup instance on (localdb)" when the full instance name is "(localdb)\\MSSQLLocalDB"

I'm trying to do a simple select query to my database with node.js via the mssql package, but the it seems that the server property of my connection string doesn't take the full string and stop at (localdb) instead of the full server name which is (localdb)\MSSQLLocalDB
Code :
let sql = require('mssql');
sql.connect({
server:'(localdb)\\MSSQLLocalDB',
database:'xxx',
options:{
trustedConnection:true
}
},function(err){
if(err) console.log(err);
var request = new sql.Request();
request.query('simple query',function(err,recordset){
if(err) console.log(err);
res.send(recordset);
})
})
Error :
ConnectionError: Failed to lookup instance on (localdb) - getaddrinfo ENOTFOUND (localdb)
Was there a change on how mssql in node checks the instance link or am I doing something wrong?
Thanks a lot ! :)

Error connecting NodeJS and SQL Server - Could not connect (sequence)

I'm trying to create a connection with SQL Server and NodeJS as shown below (SQL Server installed locally -> localhost):
It's throwing the following error: ERROR: Failed to connect to MY_SERVER:1433 - Could not connect (sequence)
This didn't work either.
I thought it was a problem with the SQL Server installation, but via Python It works well on the same database.
I'm using SQL Server 2019 and latest NodeJS version.
REFERENCE:
https://learn.microsoft.com/en-us/sql/connect/node-js/step-3-proof-of-concept-connecting-to-sql-using-node-js?view=sql-server-ver15
How to fix this error?
var config = {
server: 'MY_SERVER',
authentication: {
type: 'default',
options: {
userName: 'sa',
password: 'admin'
}
},
options: {
encrypt: false, // I've already tried with "true" also.
database: 'MyDataBase'
}
};
var connection = new Connection(config);
connection.on('connect', function(err) {
if (err) {
console.log("Error: ", err.message);
} else {
console.log("Connected");
}
});
connection.connect();
I got it!! Basically, for NodeJS get connection to SQLServer, the TCP/IP Protocol must be enabled for the MSSQLSERVER Instance, in the SQL Server network settings.
Ref.:
Nodejs connection with mssql showing error
https://store.oceansystems.com/knowledgebase/quickdme-faqs/sql-server-sql-express/configure-sql-express-server-host-enable-tcp-ip-firewall-settings/

Expressjs Not Returning All Properties From msnodesqlv8 err Object

In the code below, I'm causing an error (for illustration) by attempting to connect to a database that doesn't exist on the SQL Server.
const sql = require("msnodesqlv8");
const express = require("express");
const app = express();
// Start server and listen on http://localhost:8081/
const server = app.listen(8081, function () {
console.log("Server listening on port: %s", server.address().port)
});
const connectionString = "Server=.;Database=DatabaseThatDoesNotExist;Trusted_Connection=Yes;Driver={SQL Server Native Client 11.0}";
const sql = "SELECT * FROM dbo.Table1";
app.get('/', function (req, res)
{
sql.query(connectionString, sql, (err, recordset) => {
if(err)
{
console.log(err);
res.status(500).send(err);
return;
}
// else
res.json(recordset);
});
})
The result of console.log(err); lists the error objects with four properties (code, message, sqlstate and stack):
Array(2) [Error: [Microsoft][SQL Server Native Client 11.0][…, Error: [Microsoft][SQL Server Native Client 11.0][…]
codefile.js:33
length:2
__proto__:Array(0) [, …]
0:Error: [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'DOMAIN\USER'.
code:18456
message:"[Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'DOMAIN\USER'."
sqlstate:"28000"
stack:"Error: [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'DOMAIN\USER'."
__proto__:Object {constructor: , name: "Error", message: "", …}
1:Error: [Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot open database "MyDatabase" requested by the login. The login failed.
code:4060
message:"[Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot open database "MyDatabase" requested by the login. The login failed."
sqlstate:"42000"
stack:"Error: [Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot open database "MyDatabase" requested by the login. The login failed."
__proto__:Object {constructor: , name: "Error", message: "", …}
However, the result of res.status(500).send(err); (seen in the client) only contains two properties (sqlstate and code):
[
{
"sqlstate": "28000",
"code": 18456
},
{
"sqlstate": "42000",
"code": 4060
}
]
How do I get express to return the message and stack properties also?
The problem is that Error does not have enumerable fields and therefore message and stack don't get serialized to JSON.
See this question for ways to serialize the error object.
A quick solution for your case: res.status(500).send({message: err.message, stack: err.stack});
But this is of course not scalable. A better approach would be to configure the json replacer for express:
app.set('json replacer', replaceErrors);
And use the replacer implementation from the linked question excellent answer by #jonathan-lonowski:
function replaceErrors(key, value) {
if (value instanceof Error) {
var error = {};
Object.getOwnPropertyNames(value).forEach(function (key) {
error[key] = value[key];
});
return error;
}
return value;
}

Connecting to redis using kue always creates a connection to localhost

I can't connect to Redis using kue, I've followed this article, practically I'm creating the connection by using the kue redis client, and the connection code is this:
var kue = require('kue'),
redis = require('kue/node_modules/redis');
app.redisClient = redis.createClient('6379', 'remoteip',{});
app.redisClient.on('error', function (err) {
console.log('Redis error encountered', err);
});
app.redisClient.on('end', function() {
console.log('Redis connection closed');
});
kue.redis.createClient = function() {
console.log('client ------------------',app.redisClient);
return app.redisClient;
};
and it seems like Kue is trying to connect to a local Redis (which I don't have installed), because I'm getting this exception:
Error: Redis connection to 127.0.0.1:6379 failed - connect
ECONNREFUSED
I've read this post and it seems like the issue has been resolved in version 0.8 and I'm using 0.8.11 :/, finally I also wanted to override the client using a different client instance by using redis nodejs without any luck, because I'm getting the same error.
any help will be more than appreciated.
thanks!
I used a different way to setup the connection and it works so far this is what I've done:
app.redisClient = redis.createClient(config.redisPort, config.redisUrl,{});
app.redisClient.on('connect', function () {
console.info('successful connection to redis server');
});
app.redisClient.on('error', function (err) {
console.log('Redis error encountered', err);
});
app.redisClient.on('end', function() {
console.log('Redis connection closed');
});
kue.app.listen(config.kuePort);
app.jobs = kue.createQueue({
redis: {
createClientFactory: function(){
return app.redisClient;
}
}
});

Resources