Reconnect MongoDB using MongoSkin ( Nodejs ) - node.js

I have been using MongoSkin( Nodejs ) plugin to connect Mongodb as below.
var db = mongo.db(dbconfig.mongo_ip+'/'+dbconfig.mongo_db_name);
db.collection('myprofile').findOne({_id:memberid}, function(err, session){
if(err){
db.close();
console.log(err.stack);
}
})
If i got the mongo connection error from node.js, then how do i reconnect the same using mongoskin,
Mongo Connect errr: Error: failed to connect to [localhost:27017]
at [object Object].<anonymous> (/usr/local/lib/node_modules/mongoskin/node_modules/mongodb/lib/mongodb/connection/server.js:184:51)
at [object Object].emit (events.js:45:17)
at [object Object].<anonymous> (/usr/local/lib/node_modules/mongoskin/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:110:14)
at [object Object].emit (events.js:45:17)
at Socket.<anonymous> (/usr/local/lib/node_modules/mongoskin/node_modules/mongodb/lib/mongodb/connection/connection.js:301:10)
at Socket.emit (events.js:42:17)
at Array.33 (net.js:799:27)
at EventEmitter._tickCallback (node.js:108:26)
Please help me on this.

A good place to start is
https://github.com/kissjs/node-mongoskin/blob/master/test/db.js
but I assume you need to call open on db under mongoskin as well as in the driver.

Related

Redis -nodejs simple program -ERROR

I am getting error for even a simple nodejs redis commands.
This is the error I am getting.
/home/veera/Radha/node_modules/redis-client/lib/redis-client.js:394
var callback = originalCommand[originalCommand.length - 1];
^
TypeError: Cannot read property 'length' of undefined
at Client.onReply_ (/home/veera/Radha/node_modules/redis-client/lib/redis-client.js:394:51)
at maybeCallbackWithReply (/home/veera/Radha/node_modules/redis-client/lib/redis-client.js:143:30)
at ReplyParser.feed (/home/veera/Radha/node_modules/redis-client/lib/redis-client.js:183:29)
at Socket. (/home/veera/Radha/node_modules/redis-client/lib/redis-client.js:337:28)
at Socket.emit (events.js:95:17)
at Socket. (_stream_readable.js:765:14)
at Socket.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:427:10)
at emitReadable (_stream_readable.js:423:5)
at readableAddChunk (_stream_readable.js:166:9)
And the code is,
var client = require("./redis-node-client/lib/redis-client").createClient();
client.set('test', 'data');
My redis version is ,
redis-server --version
Redis server v=3.0.4 sha=00000000:0 malloc=jemalloc-3.6.0 bits=64 build=4e722bd58502cba0
https://github.com/fictorial/redis-node-client/issues/26 - same issue has been discussed long time back but the version they are discussing is 1.2.
My version is 3.0.4.
Please help me to fix this issue.
It looks like this library is no more supported. ( Description itself says abandoned project.) Unless you have strict requirement, use actively developed and production ready library like node-redis.
Example using node-redis
var client = require("node-redis").createClient();
client.set('test', 'data');
client.get('test',function(err, data){
if(err) console.log(err);
else console.log('reply from redis:' + data);
});
client.quit();

Following tutorial to connect postgres db to node project - failing at running script to create table

i am following a guide on how to use postgres + node. I have gotten to the point of creating table creation script.
here is the code I am using from the tutorial:
var pg = require('pg');
var connectionString = process.env.DATABASE_URL || 'postgres://postgres:postgres#localhost:5432/todo';
var client = new pg.Client(connectionString);
client.connect();
var query = client.query('CREATE TABLE items(id SERIAL PRIMARY KEY, text VARCHAR(40) not null, complete BOOLEAN)');
query.on('end', function() { client.end(); });
However, when I do the step of node models/database.js, I receive the following error:
ram#ram-windows-xp-ubuntu:~/Development/Web/Projects/node-postgres$ node models/database.js
events.js:85
throw er; // Unhandled 'error' event
^
error: password authentication failed for user "postgres"
at Connection.parseE (/home/ram/Development/Web/Projects/node-postgres/node_modules/pg/lib/connection.js:539:11)
at Connection.parseMessage (/home/ram/Development/Web/Projects/node-postgres/node_modules/pg/lib/connection.js:366:17)
at Socket.<anonymous> (/home/ram/Development/Web/Projects/node-postgres/node_modules/pg/lib/connection.js:105:22)
at Socket.emit (events.js:107:17)
at readableAddChunk (_stream_readable.js:163:16)
at Socket.Readable.push (_stream_readable.js:126:10)
at TCP.onread (net.js:538:20)
I am new to using the ubuntu and I tried to follow the steps from this guide but confused. Sorry for the bother friends please if you can do the needful I will be humbled.

MongoDB: Server sockets closed after a few minutes

I am working with multiple AWS intances connected to the same mongo database (inside Compose.io Elastic deployment) but I keep getting the error server <url>:<port> sockets closed after a few minutes. Can anyone give me any hint about what may be wrong with the connection code?
CONNECTION CODE
var url = "mongodb://<user>:<password>#<url1>:<port1>,<url2>:<port2>/<dbName>?replicaSet=<replicaSetName>";
var options = {
server : {"socketOptions.keepAlive": 1},
replSet : { "replicaSet": <replicaSetName>, "socketOptions.keepAlive": 1 }
};
MongoClient.connect(url, options, function(err, db) { ... });
ERROR MESSAGE
Potentially unhandled rejection [2] MongoError: server <url>:<port> sockets closed
at null. (/var/app/current/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:328:47)
at g (events.js:199:16)
at emit (events.js:110:17)
at null. (/var/app/current/node_modules/mongodb/node_modules/mongodb-core/lib/connection/pool.js:101:12)
at g (events.js:199:16)
at emit (events.js:110:17)
at Socket. (/var/app/current/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:142:12)
at Socket.g (events.js:199:16)
at Socket.emit (events.js:107:17)
at TCP.close (net.js:485:12)

MongoError: write EPIPE

I'm using Node.JS + Mongoose + MongoDB. Had my app working ok until now, now when trying to save a document:
Saving operation:
doc.save(function(err, d){
console.log(err,d);
{"name":"MongoError","message":"write EPIPE"}
Error stack:
{ [MongoError: server localhost:27017 received an error {"name":"MongoError","message":"write EPIPE"}]
name: 'MongoError',
message: 'server localhost:27017 received an error {"name":"MongoError","message":"write EPIPE"}' } undefined
error: TypeError: Cannot read property 'url' of undefined
at EventEmitter.<anonymous> (/home/ec2-user/c/index.js:160:61)
at EventEmitter.<anonymous> (/home/ec2-user/c/node_modules/mongoose/node_modules/mpromise/lib/promise.js:174:48)
at EventEmitter.emit (events.js:95:17)
at Promise.safeEmit (/home/ec2-user/c/node_modules/mongoose/node_modules/mpromise/lib/promise.js:81:21)
at Promise.reject (/home/ec2-user/c/node_modules/mongoose/node_modules/mpromise/lib/promise.js:109:15)
at Promise.error (/home/ec2-user/c/node_modules/mongoose/lib/promise.js:94:15)
at Promise.resolve (/home/ec2-user/c/node_modules/mongoose/lib/promise.js:112:24)
at /home/ec2-user/c/node_modules/mongoose/lib/document.js:1555:39
at handleError (/home/ec2-user/c/node_modules/mongoose/node_modules/hooks-fixed/hooks.js:40:22)
at next_ (/home/ec2-user/c/node_modules/mongoose/node_modules/hooks-fixed/hooks.js:75:26)
Looks like I'm able to read data, but not to write new documents. Mongo is app and running. I've tried so far, restarting the DB, with no good results.
Just tried to insert from mongo client and worked
> db.mysecretcollection.insert({url:"sampletestremoveme"});
WriteResult({ "nInserted" : 1 })
So it seems to be a Node.js / Mongoose issue.
It seems to me that the document was too big, actually I was able to save it after removing one of its fields. The weird thing is that mongoose should catch that with "Error: Document exceeds maximum allowed bson size of XXX bytes" exception, but that was never thrown.

Syntax error when using sequelize postgres

sequelize: 1.7.0
pg: 2.11.1
posgreSql: 9.3 (pg server on my pc)
I have app that is using following config:
db: 'mysql://user:pass#localhost/dbname'
And it works with sequelize and mysql dialect.
When I change it to:
db: 'postgres://user:pass#localhost/dbname'
and dialect to postgres and try to sequelize.sync() I get following error:
{
"message":"syntax error at or near \"(\"",
"name":"error",
"stack":"error: syntax error at or near \"(\"\n at Connection.parseE (/Users/me/Desktop/node/myapp/node_modules/pg/lib/connection.js:561:11)\n at Connection.parseMessage (/Users/me/Desktop/node/myapp/node_modules/pg/lib/connection.js:390:17)\n at [object Object].<anonymous> (/Users/me/Desktop/node/myapp/node_modules/pg/lib/connection.js:92:20)\n at EventEmitter.emit (events.js:95:17)\n at Socket.<anonymous> (_stream_readable.js:746:14)\n at EventEmitter.emit (events.js:92:17)\n at emitReadable_ (_stream_readable.js:408:10)\n at emitReadable (_stream_readable.js:404:5)\n---------------------------------------------\n at module.exports.CustomEventEmitter.failure.CustomEventEmitter.fail.CustomEventEmitter.error (/Users/me/Desktop/node/myapp/node_modules/sequelize/lib/emitters/custom-event-emitter.js:93:10)\n at exec (/Users/me/Desktop/node/myapp/node_modules/sequelize/lib/query-chainer.js:92:14)\n at onError (/Users/me/Desktop/node/myapp/node_modules/sequelize/lib/query-chainer.js:72:11)\n at EventEmitter.emit (events.js:95:17)\n at module.exports.CustomEventEmitter.emit (/Users/me/Desktop/node/myapp/node_modules/sequelize/lib/emitters/custom-event-emitter.js:61:33)\n at [object Object].<anonymous> (/Users/me/Desktop/node/myapp/node_modules/sequelize/lib/emitters/custom-event-emitter.js:131:17)\n---------------------------------------------\n at module.exports.CustomEventEmitter.failure.CustomEventEmitter.fail.CustomEventEmitter.error (/Users/me/Desktop/node/myapp/node_modules/sequelize/lib/emitters/custom-event-emitter.js:93:10)\n at exec (/Users/me/Desktop/node/myapp/node_modules/sequelize/lib/query-chainer.js:92:14)\n at onError (/Users/me/Desktop/node/myapp/node_modules/sequelize/lib/query-chainer.js:72:11)\n at EventEmitter.emit (events.js:95:17)\n at module.exports.CustomEventEmitter.emit (/Users/me/Desktop/node/myapp/node_modules/sequelize/lib/emitters/custom-event-emitter.js:61:33)\n at [object Object].<anonymous> (/Users/me/Desktop/node/myapp/node_modules/sequelize/lib/emitters/custom-event-emitter.js:131:17)",
"code":"42601"
},
"msg":"uncaughtException",
"time":"2014-03-31T14:17:29.496Z",
"v":0
}
What I am doing wrong?
Before I created dbname database in pg there was error missing database so I guess sequelize is connected to pg but it generates some syntax errors in pgsql code??
the problem was that in model schema I had Sequelize.INTEGER(11) - and postgres does not allow to pass 11 as a size of INTEGER - this will be solved in 2.x.x version of Sequelize (You can find issue corresponding to this problem on sequelize github repo).
Sequelize and postgres work well for me. Did you set dialect, protocol, port correctly in config.js like this
{
"development":{
"username":"name",
"password":"pass",
"database":"db",
"dialect":"postgres",
"protocol":"postgres",
"host":"127.0.0.1",
"port":"5432"
},
...

Resources