Getting error while running node-mysql tutorial - node.js

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

Related

how to resolve TypeError : this is not a typed array

var kafka = require('kafka-node');
Producer = kafka.Producer;
KeyedMessage = kafka.KeyedMessage;
client = new kafka.Client();
producer = new Producer(client);
km = new KeyedMessage('key', 'message');
payloads = [
{ topic: 'topic1', messages: 'hi', partition: 0 },
{ topic: 'topic2', messages: ['hello', 'world', km] }
];
producer.on('ready', function ()
{
producer.send(payloads, function (err, data)
{
console.log(data);
});
});
producer.on('error', function (err) {});
//error for that code
baaz#bit:~$ node kafka1.js
/home/baaz/node_modules/kafka-node/lib/codec/snappy.js:18
var SNAPPY_MAGIC = Buffer.from(SNAPPY_MAGIC_BYTES).toString('hex');
^
TypeError: this is not a typed array.
at Function.from (native)
at Object.<anonymous> (/home/baaz/node_modules/kafka-node/lib/codec/snappy.js:18:29)
at Module._compile (module.js:425:26)
at Object.Module._extensions..js (module.js:432:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (/home/baaz/node_modules/kafka-node/lib/codec/index.js:4:19)
at Module._compile (module.js:425:26)
Short Answer :
Either increase the node version above 4.5 or
use "kafka-node": "1.6.2" or lesser
Long Answer
with node 4.5 and above, node deprecated the old way of creating buffer via a constructor, so all node js version below 4.5 do not have new that support. and kafka-node version > 1.6.2 uses the buffer.from to create the buffer so we have the compatibility issues.
Cheers
Happy coding

unable to restart my server using node server/index.js

I am trying to run the server using :
node server/index.js
But I am getting this :
SyntaxError: Unexpected token >
at Module._compile (module.js:439:25)
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:902:3
The index .js file is failing on :
db.each("SELECT name FROM sqlite_master WHERE type = 'table'", (err, data) => {
tables.push(data.name);
});
Any idea what may be wrong ?
Try replacing
db.each("SELECT name FROM sqlite_master WHERE type = 'table'", (err, data) => {
tables.push(data.name);});
with
db.each("SELECT name FROM sqlite_master WHERE type = 'table'", function(err, data) {
tables.push(data.name);});
as it seems your node version is not supporting this syntax.

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")

Using node.js to connect to impala via jdbc

I'm using the npm module jdbc to try to connect to Cloudera Impala.
I have tried both the apache hive-jdbc-0.13.1-cdh5.3.3.jar for hiveserver2
as well as the Cloudera JDBC ImpalaJDBC41.jar
var jdbc = new (require('jdbc'));
var config = {
libpath: '/home/ubuntu/downloads/impala_jdbc/hive-jdbc-0.13.1-cdh5.3.3.jar',
libs:'/home/ubuntu/downloads/impala_jdbc/lib/*.jar',
drivername: 'org.apache.hive.jdbc.HiveDriver',
url: 'jdbc:hive2://54.172.122.6:21050/;auth=noSasl' + process.argv[2]
};
//Initialize jdbc object
jdbc.initialize(config, function(err, res){ if (err){ console.log(err); } });
jdbc.open(function(err, conn) {
if (conn) {
//Run first query
jdbc.executeQuery('show databases', function(err, results){
if (err){ console.log(err); }
else if (results) { console.log(results); }
});
}else{
console.log('Connection object:' + conn);
console.log(err);
}
});
jdbc.close(function(err){
if(err) { console.log(err); }
else { console.log('Connection closed successfully!');}
});
whenever I run it (after correcting for each driver path)
I get the following
/home/ubuntu/PlayArea/ImpalaJDBC/node_modules/jdbc/lib/jdbc.js:22
java.classpath.push.apply(java.classpath, self._config.libs);
^
TypeError: Function.prototype.apply: Arguments list has wrong type
at JDBCConn.initialize (/home/ubuntu/PlayArea/ImpalaJDBC/node_modules/jdbc/lib/jdbc.js:22:24)
at Object.<anonymous> (/home/ubuntu/PlayArea/ImpalaJDBC/server.js:12:6)
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:935:3
I'm assuming that my libs path is not finding all the jars needed.

Elmongo plugin for full text search

I'm trying to use the Elmongo plugin for Node.js and I'm having some trouble with the plugin. Here is my code:
var mongoose = require('mongoose');
var elmongo = require('elmongo');
var Schema = mongoose.Schema;
var twitterSchema = new Schema ({
id: {type: Number, index: {unique: true, dropDups: true}},
created_at: Date,
user: [{
id: Number,
name: String,
screen_name: String,
location: String
}],
text: String,
keywords: []
});
twitterSchema.plugin(elmongo);
var Tweets = mongoose.model('Tweets', twitterSchema);
twitterSchema.sync(function(err, numSynced){
console.log("number of tweets synced: " + numSynced);
});
twitterSchema.search({query: 'flu'}, function(err, results){
console.log('search results: ' + results);
});
exports.Document = function(db) {
return db.model('Tweets');
};
The error message I'm getting is:
module.js:340
throw err;
^
Error: Cannot find module 'elmongo'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Users/Documents/UCIMedCenterProject/MedStream-feature.sentiment-analysis/models.js:2:15)
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 Module.require (module.js:364:17)
I looked up some of the errors and what I get is that I need to reinstall the module, but that didn't work either. Any help?
You should install this module in your project directory (go to your project directory in command line and then run "npm install elmongo")
You should check inside node_modules folder if any folder with name elmongo is present. If not try installing the module again.
Try installing as administrator.
sudo npm install elmongo

Resources