Node.js + node-tds + Sql Server - node.js

I'm trying to follow the node-tds example on how to connect and retrieve data from Sql server with node.js http://cretz.github.com/node-tds/
I got no error on connect, but when I try to retrieve data I get the following:
"Error: Client must be in LOGGED IN state before executing sql
at TdsClient.sqlBatch (C:\Program Files (x86)\nodejs\node_modules\tds\lib\tds-client.js:101:13)
at Statement.<anonymous> (C:\Program Files (x86)\nodejs\node_modules\tds\lib\tds.js:256:37)
at Statement.execute (C:\Program Files (x86)\nodejs\node_modules\tds\lib\tds.js:2:59)
at Object.<anonymous> (C:\Program Files (x86)\nodejs\edas\app.js:67:6)
at Module._compile (module.js:446:26)
at Object..js (module.js:464:10)
at Module.load (module.js:353:31)
at Function._load (module.js:311:12)
at Array.0 (module.js:484:10)
at EventEmitter._tickCallback (node.js:190:38)"
I'm logged in, so… I can't figure out what is happening.

You should execute your statement when the connection has already established, i.e.
var tds = require("tds");
var conn = new tds.Connection({
host: "localhost",
port: 1433,
userName: "user",
password: "secret",
database: "MyDb"
});
conn.connect(function(error) {
if (error != null) {
console.error("Received error", error);
} else {
console.log("Now connected, can start using");
}
});
conn.on("connect", function() {
var statement = conn.createStatement("SELECT ProductID, ProductName FROM Products");
statement.on("row", function(row) {
console.log("Received row: ", row.getValue(0));
});
statement.execute();
});

Related

No AuthProvider for DEFAULT defined with mongoose 6

When updating to Mongoose 6 with Mongodb nodejs driver version 4, I have the error below:
MongoInvalidArgumentError: No AuthProvider for DEFAULT defined.
at prepareHandshakeDocument (/home/arch/git/project/node_modules/mongodb/lib/cmap/connect.js:153:29)
at performInitialHandshake (/home/arch/git/project/node_modules/mongodb/lib/cmap/connect.js:63:5)
at /home/arch/git/project/node_modules/mongodb/lib/cmap/connect.js:25:9
at callback (/home/arch/git/project/node_modules/mongodb/lib/cmap/connect.js:244:9)
at Socket.connectHandler (/home/arch/git/project/node_modules/mongodb/lib/cmap/connect.js:282:9)
at Object.onceWrapper (events.js:519:28)
at Socket.emit (events.js:400:28)
at Socket.emit (domain.js:470:12)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1134:10)
From previous event:
at NativeConnection.Connection.openUri (/home/arch/git/project/node_modules/mongoose/lib/connection.js:778:19)
at /home/arch/git/project/node_modules/mongoose/lib/index.js:330:10
at promiseOrCallback (/home/arch/git/project/node_modules/mongoose/lib/helpers/promiseOrCallback.js:10:12)
at Mongoose._promiseOrCallback (/home/arch/git/project/node_modules/mongoose/lib/index.js:1151:10)
at Mongoose.connect (/home/arch/git/project/node_modules/mongoose/lib/index.js:329:20)
at connectToDb (/home/arch/git/project/core/configs/database.js:101:8)
at createServer (/home/arch/git/project/app.js:65:16)
at Object.<anonymous> (/home/arch/git/project/app.js:262:3)
at Module._compile (internal/modules/cjs/loader.js:1072:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47
I don't have any authentication set up (this is my local development environment) so I don't understand where is the issue comming from. Here is the relevant connection code:
let connectionURL = 'mongodb://';
const connectionOptions = {
authSource: options.authSource,
};
if (options.user && options.password) {
connectionURL += `${options.user}:${options.password}#`;
}
connectionURL += `${options.host}/${dbName}?authSource=${options.authSource}`;
mongoose
.connect(connectionURL, connectionOptions, function onConnected(err) {
if (err) {
return cb(new Error(`Can not connect to database ${dbName}`));
}
return cb(null, mongoose.connection);
});
For a passwordless/userless connection, making sure the connection url doesn't contain authSource worked for me. This worked somehow before the version 4 of the driver, but not any more.
Also, either choose wether to pass options in the object or in the connection string. As the doc says:
Best practice is to put options that likely differ between development and production, like replicaSet or ssl, in the connection string, and options that should remain constant, like connectTimeoutMS or poolSize, in the options object
const connectionOptions = {};
if (options.user && options.password) {
connectionOptions.user = options.user;
connectionOptions.password = options.password;
connectionOptions.authSource = options.authSource;
}
const connectionURL = `mongodb://${options.host}/${dbName}`;
mongoose
.connect(connectionURL, connectionOptions, function onConnected(err) {
if (err) {
return cb(new Error(`Can not connect to database ${dbName}`));
}
return cb(null, mongoose.connection);
});
};

Handshake inactivity timeout error AWS node.js

i'm quite new with node.js. usually i'm connect the AWS through java but this time i needed to change from java based to node.js with lambda service. i have simple connection from node.js to AWS RDS Database,
var mysql = require('mysql');
var config = require('./config.json');
var pool = mysql.createPool({
host: config.dbhost,
user: config.dbuser,
password: config.dbpassword,
database: config.dbname,
});
pool.getConnection(function(err, connection) {
// Use the connection
if (err) {
console.log(err);
} else {
connection.query('Select 1+1 as numberss', function (error, results, fields) {
// And done with the connection.
connection.release();
// Handle error after the release.
if (error) throw error;
else console.log(results);
process.exit();
// Don't use the connection here, it has been returned to the pool
});
}
});
i've tried so many ways that i'm search in internet like adding timeout, check host information and etc.
but it's seem to return
{ Error: Handshake inactivity timeout
at Handshake.<anonymous> (/Users/HAUZDevTeam/HAUZ-HQ-Lambda/node_modules/mysql/lib/protocol/Protocol.js:160:17)
at Handshake.emit (events.js:189:13)
at Handshake._onTimeout (/Users/HAUZDevTeam/HAUZ-HQ-Lambda/node_modules/mysql/lib/protocol/sequences/Sequence.js:124:8)
at Timer._onTimeout (/Users/HAUZDevTeam/HAUZ-HQ-Lambda/node_modules/mysql/lib/protocol/Timer.js:32:23)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)
--------------------
at Protocol._enqueue (/Users/HAUZDevTeam/HAUZ-HQ-Lambda/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/Users/HAUZDevTeam/HAUZ-HQ-Lambda/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at PoolConnection.connect (/Users/HAUZDevTeam/HAUZ-HQ-Lambda/node_modules/mysql/lib/Connection.js:116:18)
at Pool.getConnection (/Users/HAUZDevTeam/HAUZ-HQ-Lambda/node_modules/mysql/lib/Pool.js:48:16)
at Object.<anonymous> (/Users/HAUZDevTeam/HAUZ-HQ-Lambda/local.js:9:6)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
code: 'PROTOCOL_SEQUENCE_TIMEOUT',
fatal: true,
timeout: 10000 }
is there any way to overcome this error or is there any wrong thing that i did on my code?

TypeError: Object #<Object> has no method 'Request' with elastic.js

i tried the following sample code:
var es=require("elasticsearch");
var ejs = require("ejs");
var client = new es.Client({
host: 'localhost:9200',
log: 'trace'
});
client.ping({
requestTimeout: 3000,
// undocumented params are appended to the query string
hello: "elasticsearch"
}, function (error) {
if (error) {
console.error('elasticsearch cluster is down!');
} else {
console.log('All is well');
}
});
client.search({
index: 'message-1',
type: 'message',
body: ejs.Request().query(ejs.MatchAllQuery())
}).then(function (resp) {
var hits = resp.hits.hits;
}, function (err) {
console.trace(err.message);
});
i am getting the following error when i try to run: node metric.js
/home/karunakar/test/metricagg/metric.js:27
body: ejs.Request().query(ejs.MatchAllQuery())
^
TypeError: Object #<Object> has no method 'Request'
at Object.<anonymous> (/home/karunakar/test/metricagg/metric.js:27:13)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:929:3
i could not locate the problem.
-- Thanks
You want the elastic.js module and not ejs, which is a templating engine module.
You can keep the first require("elasticsearch") but the second one should be require("elastic.js") instead of require("ejs")

Getting error while running node-mysql tutorial

I am running the code in the tutorial part of node-mysql and its giving me error
Code
var MySQLPool = require("mysql-pool").MySQLPool;
var pool = new MySQLPool({
poolSize: 4,
user: 'root',
password: 'root',
database: 'test'
});
pool.query("SELECT 'Hello, World!' AS hello", function(err, rows, fields) {
if(err) throw err;
console.log(rows[0].hello);
});
for(var i = 0; i < 10; ++i) {
pool.query("SELECT SLEEP(2), ? AS i", [i], function(err, rows, fields) {
if(err) throw err;
console.log("Slept: " + rows[0].i);
});
}
ERROR:
/home/comapq/Works/Nodejs/Codes/node_modules/mysql-pool/lib/mysql-pool/pool.js:158
for(var key in Client.prototype) {
^
TypeError: Cannot read property 'prototype' of undefined
at MySQLPool._populate (/home/comapq/Works/Nodejs/Codes/node_modules/mysql-pool/lib/mysql-pool/pool.js:158:23)
at new MySQLPool (/home/comapq/Works/Nodejs/Codes/node_modules/mysql-pool/lib/mysql-pool/pool.js:44:7)
at Object. (/home/comapq/Works/Nodejs/Codes/test-mysql-3.js:2:12)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
After a bit of research I found the problem. You probably installed mysql like this:
npm install mysql
This grabs the latest mysql package that is 2.0.0-alpha5(development phase, mostly untested). You need to install the latest stable version of mysql:
npm install mysql#0.9.6

Nodejs Cassandra Client [node-cassandra-client]

I tried connecting to my cassandra cluster [version 1.0.6] via nodejs using node-cassandra-client
This is my sample script
var Connection = require('cassandra-client').Connection;
var con = new Connection({host:'x.x.x.x', port:9160, keyspace:'Stats', timeout:10000});
console.log(con);
con.execute('UPDATE TestCF ?=? WHERE key=?', ['cola', '1', '20120132'], function(err) {
if (err) {
console.log("Failed");
} else {
console.log("success");
}
});
On executing the script
The "sys" module is now called "util". It should have a similar interface.
node-cassandra-client.driver: connecting x.x.x.x:9160 {}
{ validators: {},
client: null,
connectionInfo:
{ host: 'x.x.x.x',
port: 9160,
keyspace: 'Stats',
timeout: 10000 },
timeout: 10000 }
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
TypeError: Cannot call method 'execute_cql_query' of null
at [object Object].execute (/home/tamil/workspace/TestProjects/node-cass/node_modules/cassandra-client/lib/driver.js:367:17)
at Object.<anonymous> (/home/tamil/workspace/TestProjects/node-cass/index.js:5:5)
at Module._compile (module.js:432:26)
at Object..js (module.js:450:10)
at Module.load (module.js:351:31)
at Function._load (module.js:310:12)
at Array.0 (module.js:470:10)
at EventEmitter._tickCallback (node.js:192:40)
My nodetool stats
Address DC Rack Status State Load Owns Token
x.x.x.x datacenter1 rack1 Up Normal 1.03 MB 100.00% 0
What would be the error reason? and Need some help to fix this
the client inside your connection is null. You need to connect() first. Here's an example (ignores all errors):
var con = new Connection({host:'x.x.x.x', port:9160, keyspace:'Stats', timeout:10000});
con.connect(function(err) {
assert.ifError(err);
con.execute('SELECT COUNT(*) FROM TestCF', [], function(err, rows) {
con.close(function(err) {
// you're done now.
});
});
});
I recommend using async to handle the ordering of connct, query, close, etc.
Looks like some kind of problem with the node.js thrift driver. I've had more luck with Helenus.

Resources