voltdb-client-nodeJS doesn't work about #AdHoc proceduer - node.js

I followed the voltdb-client-nodeJS, and implement the #AdHoc query function, but the "read" function didn't callbacked, so that can't get any queried results, can't determine if the connection is successful, too.
[callProcedure code]
var query = resultsProc.getQuery();
query.setParameters(["select * from Q_SHIPPINGCARRIERHISTORY_STREAMING where TrackingNumber=431476575751"]);
client.call(query, function read(errorCode, eventCode, results){
...
}, function write(errorCode, eventCode, results){
...
});
[connect DB code]
var config = new VoltConfiguration();
config.host = "s7biapp26";
config.port = 8080;
var client = new VoltClient([config]);
client.connect(function(code, event, results){
...
});
On the second attempt, can't require('voltjs') or require('volt') after npm install voltjs(version:voltjs#0.2.0).
So, could you provide a more detailed documentation about the voltdb-client-nodeJS, or paste a simpler demo of #AdHoc, thank you very much!

The key is connected voltdb failed by error code in connect callback function, I tried to remove the port, then connected success!
The secondly, requiring the module should require('voltjs/lib/client').
referenced the links:
https://github.com/VoltDB/voltdb-client-nodejs/issues/12
https://forum.voltdb.com/forum/voltdb-discussions/building-voltdb-applications/577-nodejs-client-for-helloworld

Related

nodeschool:learnyoumongo "FIND" lesson fails before I write anything - config?

Currently the only thing I have in my file is the following:
var mongo = require('mongodb').MongoClient,
url = 'mongodb://localhost:27017/learnyoumongo';
console.log(mongo);
When I run the verify command, I get the following error:
/usr/local/lib/node_modules/learnyoumongo/exercises/find/exercise.js:37
db.collection('parrots').remove({}, function(err) {
^
TypeError: Cannot read property 'collection' of undefined
at Exercise.<anonymous> (/usr/local/lib/node_modules/learnyoumongo/exercises/find/exercise.js:37:5)
at next (/usr/local/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/exercise.js:260:17)
at Exercise.end (/usr/local/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/exercise.js:266:5)
at Workshopper.end (/usr/local/lib/node_modules/learnyoumongo/node_modules/workshopper/workshopper.js:191:12)
at Workshopper.done (/usr/local/lib/node_modules/learnyoumongo/node_modules/workshopper/workshopper.js:323:19)
at Exercise.<anonymous> (/usr/local/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/exercise.js:149:14)
at /usr/local/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/exercise.js:136:16
at Exercise.<anonymous> (/usr/local/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/filecheck.js:10:14)
at FSReqWrap.oncomplete (fs.js:95:15)
When I took a look at the exercises.js file, I see the error is pointing to the .addCleanup function and the db it is trying to close is undefined.
This seems like a connection/configuration error, but I passed the first two modules. Can anyone help?
Update
This is definitely a connection error. The previous scenario was created using the command in the workshop module mongod --port 27017 --dbpath=./data, however when I opened a new terminal tab and just ran mongo without any arguments, the verify command actually output the "Actual/Expected" evaluation and module results.
To the user who asked for the rest of the script, please understand if you are unfamiliar with nodeschool that this is an entire repository with module based automated/interactive tutorials, so this is not all of the code. In any case, here is what you requested:
var mongo = require('mongodb').MongoClient
, exercise = require('workshopper-exercise')()
, filecheck = require('workshopper-exercise/filecheck')
, execute = require('workshopper-exercise/execute')
, comparestdout = require('workshopper-exercise/comparestdout')
exercise = filecheck(exercise)
exercise = execute(exercise)
exercise = comparestdout(exercise)
var db, url = 'mongodb://localhost:27017/learnyoumongo'
exercise.addSetup(function(mode, cb) {
var self = this
this.submissionArgs = [3]
this.solutionArgs = [3]
mongo.connect(url, function(err, _db) {
if (err) return cb(err)
db = _db
col = db.collection('parrots')
col.insert([{
name: 'Fred'
, age: 1
}, {
name: 'Jane'
, age: 3
}, {
name: 'Jenny'
, age: 10
}], cb)
})
})
exercise.addCleanup(function(mode, pass, cb) {
db.collection('parrots').remove({}, function(err) {
if (err) return cb(err)
db.close()
cb()
})
})
module.exports = exercise
I think that should be:
var mongo = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017/learnyoumongo', function(err, db) {
console.log(err);
});
There was definitely a connection problem. I'm not sure exactly where the bug was, but I reloaded learnyoumongo and reinstalled a couple of node packages. When I did that I had to go back and change permissions on the data directory again.
My recommendation if anyone else faces strange config errors, is to try to reinstall. Then when you connect, it doesn't hurt to re-verify the CONNECT module to make sure you have a good connection when you start your work space.

Unable to deploy nodejs-mongodb project on heroku

My connection file looks like this:
var mongo = require('mongodb');
var dbName = 'KrishiMoney';
var mongoUri = process.env.MONGOLAB_URI
/* establish the database connection */
var db ;
mongo.MongoClient.connect(mongoUri, {server:{auto_reconnect:true}}, function (err,database){
if (err) {
console.log(e);
} else{
//console.log('connected to database :: ' );
db = database;
}
});
var accounts = db.collection('accounts');
This gives the following error:
var accounts = db.collection('accounts');
^
at Object.Module._extensions..js (module.js:467:10)
TypeError: Cannot call method 'collection' of undefined
My guess is that the db variable is not getting correct value.
What is the correct way to populate the db variable when connecting using MongoClient?
The callback function you pass to the MongoClient.connect method will be executed asynchronously after the connection is established so at the point when you call db.collection('accounts') db variable is undefined.
Try wrapping the rest of your code which works with the database inside the callback function, which will make sure it is executed after the connection is established

Cannot overwrite `Kitten` model once compiled

I've run into an issue with the Mongoose Getting Started guide.
I have MongoDB running and everything is working perfectly until I add the last line:
Kitten.find({ name: /^Fluff/ }, callback)
When I node server.js I get this error:
OverwriteModelError: Cannot overwrite Kitten model once compiled.
Here's the full error and my server.js.
Any idea what I'm doing wrong?
P.S. I'm running node 10.26, npm 1.4.13, express 4.4.3 & mongoose 3.8.12 on OS X 10.9.3.
You get the error because callback in Kitten.find({ name: /^Fluff/ }, callback) calls var Kitten = mongoose.model('Kitten', kittySchema); again. Change
Kitten.find({ name: /^Fluff/ }, callback)
to something like:
Kitten.find({ name: /^Fluff/ }, function(err, kittens) {
});
It doesn't make sense to call the callback function again.
I thought this could be useful to someone else who tries it...
I started looking into mongoose and tried Getting Started guide. I don't see the above error happening where it was reported. However, I did see it intermittently. If it happens, follow #3 below. Here are my observations:
Issue#1. If I copy paste the whole code I see an issue with "I don't have a name" as the single quote in don't is not escaped.
Solution#1. I tried to escape with \ and \\ but it didn't work. Google search didn't help. After some research I found that it is forgiving for single quotes outside the function. But, inside the function it won't work. So, I defined a variable outside the function. Probably better to define strings in a separate document for localization anyways. Above all, it works. :)
Issue#2. I see the issue at
fluffy.speak();
TypeError: fluffy.speak is not a function
Here since the first 'mongoose.model(...)' doesn't have this method, adding it later and re-running 'mongoose.model(...)' will not help.
Solution#2. Comment the first 'mongoose.model(...)'
Issue#3. When I copy paste the code from the web site, it is causing weird errors.
Solution#3. Just deleted all the tabs and empty spaces at the end of lines in a Notepad++. That took care of it.
Here is the code that worked for me (Needless to say, if it doesn't work, please copy paste first in notepad):
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection.error:'));
db.once('open', function(callback) {
//yay!
});
var kittySchema = mongoose.Schema({
name: String
});
//var Kitten = mongoose.model('Kitten', kittySchema);
//var silence = new Kitten({ name: 'Silence' });
//console.log(silence.name);
//console.log(silence);
var noname = "I don't have a name";
kittySchema.methods.speak = function () {
var greeting = this.name
? "Meow name is " + this.name
: noname;
console.log(greeting);
}
var Kitten = mongoose.model('Kitten', kittySchema);
var fluffy = new Kitten({ name : 'fluffy' });
fluffy.speak();

Locomotive.js throws error upon calling "locomotive.boot"

I'm trying to write tests on my locomotive.js application, literally copy/pasting code from some examples on the internet. Even so, whenever I run my tests, I get an error saying
TypeError: string is not a function
When I check the number of arguments expected by locomotive.boot (using locomotive.boot.length), it says 2... But in every single example online (go ahead, google it) the documentation seems to say 3. Does anyone know what I'm doing wrong?
Here's my code:
var locomotive = require('locomotive'),
should = require('should'),
request = require('supertest');
var app, server;
describe('Application', function() {
before(function(done) {
locomotive.boot( __dirname+"/..", "test", function(err, express) {
if (err) throw err;
app = this;
express.listen(4000, '0.0.0.0', function() {
var addr = this.address();
console.log('Server started. [Env: '+SOPS.conf.get('app:environment')+'] [Addr: '+addr.address+'] [Port: '+addr.port+']');
done();
});
server = express;
});
});
it('should have started the app', function(){
should.exist(app);
should.exist(express);
});
});
There are 2 branches on the LocomotiveJS repo:
- 0.3.x (https://github.com/jaredhanson/locomotive/tree/0.3.x)
- master (https://github.com/jaredhanson/locomotive/tree/master)
If you're using version 0.3.x your code should work, the function declaration actually shows 4 arguments: dir, env, options, callback
you can have a look at the function definition here (Locomotive.prototype.boot): 0.3.x/lib/locomotive/index.js
As of version 0.4.x (branch master) the boot function only accepts 2 arguments: env, callback
the function definition for this branch is here (Application.prototype.boot): master/lib/application.js
so your code should look something like:
locomotive.boot( "test", *yourcallback* );
Hope this helps.

Mongoose Trying to open unclosed connection

This is a simplified version of the problem, but basically I'm trying to open 2 mongodb connections with mongoose and it's giving me "Trying to open unclosed connection." error.
Code sample:
var db1 = require('mongoose');
db1.connect('my.db.ip.address', 'my-db');
var db2 = require('mongoose');
db2.connect('my.db.ip.address', 'my-db');
db2.connection.close();
db1.connection.close();
Any idea how to make it work?
connect() opens the default connection to the db. Since you want two different connections, use createConnection().
API link: http://mongoosejs.com/docs/api.html#index_Mongoose-createConnection
To add on Raghuveer answer :
I would also mention that instead of using mongoose directly (you are probably using it this way you end up on this post) :
require('mongoose').model(...);
You would use the returned connection :
var db = require('mongoose').connect('xxx', 'yyy');
db.model(...);
I get this issue while running my tests.
This is what I did to solve it.
//- in my app.js file.
try {
mongoose.connect('mongodb://localhost/userApi2'); //- starting a db connection
}catch(err) {
mongoose.createConnection('mongodb://localhost/userApi2'); //- starting another db connection
}
I had this problem doing unit test with mocha.
The problem came when I added a second test because beforeEach is called twice.
I've solved this with this code:
const mongoose = require('mongoose');
describe('Your test suite', () => {
beforeEach( () => {
if (mongoose.connection.db) {
return; // or done();
} else {
// connect to mongodb
});
describe('GET /some-path', () => {
it('It should...', () => {
});
});
describe('POST /some-path', () => {
it('It should...', () => {
});
});
});
Hope it helps you!
You are attempting to open the default connection ( which is not yet closed ) a 2nd time.
do the following instead
var db = require('mongoose'); //note only one 'require' needed.
var connectionToDb1 = db.createConnection('my.db1.ip.address', 'my-db1');
var connectionToDb2 = db.createConnection('my.db2.ip.address', 'my-db2');
Using mongoose.disconnect(fn):
mongoose.disconnect(() => {
// here it would be possible "reset" models to fix
// OverwriteModelError errors
mongoose.models = {};
// here comes your logic like registering Hapi plugins
server.register(somePlugin, callback);
});
I found this question typing the error message and despite my problem is a bit different I believe it could be useful for those using Hapi. More specifically Hapi + rest-hapi + mocha.
When running mocha with --watch option I was facing both: OverwriteModelError and Error: Trying to open unclosed connection errors.
Simple Solution -
Use mongoose.createConnection() instead of mongoose.connect()
Its occurs because of version issue

Resources