MongoClient.connect doesn't execute callback function - node.js

I'm relatively new to node.js. Trying to test connection to mongodb using mocha framework and mongodb driver.
Node.js version - 6.11.3
Mongodb driver version - 2.2.31
Mondodb version - 3.4.7
Here's my js file:
var should = require("should");
var expect = require('chai').expect;
var cfg = require('../config');
var uri = cfg.mongouri;
var MongoClient = require('mongodb').MongoClient, Logger =
require('mongodb').Logger;
Logger.setLevel('debug');
describe("mongoconnection", function () {
describe("fetch data", function () {
it("should fetch data from db", function (done) {
MongoClient.connect(uri,function(err, db) {
if (err) {
throw err;
} else {
console.log("successfully connected to the database");
}
db.close();
});
done();
});
});
});
However, this part of the code
function(err, db) {
if (err) {
throw err;
} else {
console.log("successfully connected to the database");
}
db.close();
}
never gets executed and I can't establish connection, e.g. I don't get neither console log nor exception.
Debug information:
[DEBUG-Connection:9352] 1506430786041 creating connection 0 with options [{"host":HOST,"port":PORT,"size":5,"keepAlive":true,"keepAliveInitialDelay":300000,"noDelay":true,"connectionTimeout":30000,"socketTimeout":360000,"ssl":true,"ca":null,"crl":null,"cert":null,"rejectUnauthorized":false,"promoteLongs":true,"promoteValues":true,"promoteBuffers":false,"checkServerIdentity":true}] { type: 'debug',
message: 'creating connection 0 with options [{"host":HOST,"port":PORT,"size":5,"keepAlive":true,"keepAliveInitialDelay":300000,"noDelay":true,"connectionTimeout":30000,"socketTimeout":360000,"ssl":true,"ca":null,"crl":null,"cert":null,"rejectUnauthorized":false,"promoteLongs":true,"promoteValues":true,"promoteBuffers":false,"checkServerIdentity":true}]',
className: 'Connection',
pid: 9352,
date: 1506430786041 }
also already checked that connection string is correct and I can establish connection to it via another app (groovy script executed in SoapUI).
I am stuck at this point, can someone please help me with this, thanks in advance.

You are calling done() from Mocha outside of the async callback from the MongoClient.connect. So done() is called before it can even connect to the db.
Change your code to this:
it("should fetch data from db", function (done) {
MongoClient.connect(uri,function(err, db) {
if (err) {
throw err;
} else {
console.log("successfully connected to the database");
}
db.close();
done();
});
});

Related

Unable to get data from Redis using NodeJs

I have implemented redis cache in my node server.I am running redis docker container locally.I have keys stored in redis container which I am able to see but when I am trying to access then I am failed to get data.
Below is my code:
const redis = require('redis');
let redisClient;
redisClient = redis.createClient('127.0.0.1', 6379);
redisClient.connect();
redisClient.on('error', err => {
console.log('Error ' + err);
});
redisClient.on('connect', () => {
console.log('Connected to Redis');
});
//Saving data here
redisClient.set('Region', 'Asia', (err, reply) => {
if (err) {
console.log('error', err);
}
else {
console.log(reply);
}
});
//Fetching data here
redisClient.get('Region', (err, reply) => {
if (err) {
console.log('error', err);
}
else {
console.log(reply);
}
});
Here even though data is saved successfully in redis but I am not getting console statement after saving data.And I am also unable to fetch data from redis.I am only getting Connected to Redis console log.
Someone let me know what I have done wrong in my code.So far to me code seems fine.
It looks like you are using the node-redis client. In v4, this supports promises. Using promises with async/await should make your code easier to follow:
import { createClient } from 'redis';
try {
const client = createClient();
await client.connect();
await client.set('Region', 'Asia');
const region = await client.get('Region');
console.log(`Region is ${region}`);
await client.quit();
} catch (e) {
console.error('Oh no:');
console.error(e);
}

NodeJS in AWS Lambda to Mongo Atlas - connection fails with no logs

I am JS newbie so this may be some silly trouble. I have a lambda written in NodeJS 10.x and I am trying to add MongoDB Atlas insertion. I have started with this tutorial: https://docs.atlas.mongodb.com/best-practices-connecting-to-aws-lambda/
This is my code:
const MongoClient = require('mongodb').MongoClient;
let cachedDb = null;
function connectToDatabase (uri) {
console.log('Connect to mongo database');
if (cachedDb) {
console.log('Using cached database instance');
return Promise.resolve(cachedDb);
}
return MongoClient.connect(uri)
.then(db => {
console.log('Successful connect');
cachedDb = db;
return cachedDb;
}).catch(err => {
console.log('Connection error occurred: ', err);
callback(err);
});
}
function insertUser(db, email) {
console.log('=> modify database');
return db.collection('users').insertOne({"email" : email})
.then(() => { callback(null, result); })
.catch(err => {
console.log('Insert error occurred: ', err);
callback(err);
});
}
exports.handler = (payload, context, callback) => {
const { email, password } = JSON.parse(payload.body);
context.callbackWaitsForEmptyEventLoop = false;
connectToDatabase(MONGODB_URI)
.then(db => {
console.log('Mongo connected')
insertUser(db, email);
})
.then(result => {
console.log('Mongo insert succeeded', result);
})
.catch(err => {
console.log('Mongo insert failed', err);
return responses.INTERNAL_SERVER_ERROR_500(err, callback, response);
});
console.log('finished mongo stuff');
I can see the following logs in CloudWatch:
START RequestId: 0338d336-7d33-40d5-abc7-1511f1c9ea4c Version: $LATEST
2020-01-11T12:18:00.808Z 0338d336-7d33-40d5-abc7-1511f1c9ea4c INFO Connect to mongo database
2020-01-11T12:18:00.855Z 0338d336-7d33-40d5-abc7-1511f1c9ea4c INFO finished mongo stuff
2020-01-11T12:18:01.416Z 0338d336-7d33-40d5-abc7-1511f1c9ea4c ERROR (node:8) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
END RequestId: 0338d336-7d33-40d5-abc7-1511f1c9ea4c
The item is not inserted in Atlas. I have added more verbose logging but it is not shown. If the mongo connect failed there shall be an error. But it seems that the error handlers are ignored. Where is the problem?
You are very close. Few things are missing
callback function needs to be called from your handler function as the insert function doesn't know about callback.
When you do a .then on a promise and you use curly braces you need to return whatever is in there to send it to the next then (unless you do it in a single line).
db in insert method needs to be db.db()
I would recommend to to use async/await instead of callback style. Here is the equivalent code
exports.handler = async (payload) => {
try {
const { email, password } = JSON.parse(payload.body);
const db = await connectToDatabase(MONGODB_URI);
console.log("Mongo connected");
const result = await insertUser(db, email);
console.log("Mongo insert succeeded", result);
return result;
} catch(err) {
console.error(err);
}
};

How to fix issue with sql query execution using call back

I am having problem with sql query execution using callback, I never worked on the node.js, so looking for some guidance and get going.
Tried with having database connection in same file, but unable to execute when having the database connection in another file.
DatabaseManager.js
var Tedious = require('tedious');
var Connection = Tedious.Connection;
var Request = Tedious.Request;
function connect(cb) {
var config = {
userName: 'dddff',
password: 'fsfdsf',
server: '12345.database.windows.net',
options:
{
database: '12345',
encrypt: true
}
};
var connection = new Connection(config);
connection.on('connect', function(err) {
if (err) {
console.log(err);
return;
}
console.log('CONNECTED TO DATABASE');
cb(connection);
});
}
module.exports = connect;
app.js
var connect = require('./DatabaseManager');
bot.dialog('profileDialog', (session) => {
session.send('Hello Message', session.message.text);
console.log('Creating a connection');
connect(function(connection) {
console.log('Reading rows from the Table.');
// Execute queries here and how to frame the syntax here?
connection.query("select FNAME from StudentProfile where ID=1"),
function(err, result, fields) {
if (err) throw err;
console.log(result);
}
});
})
I expect the output of select statement result on the console.

How to handle MongoDB Client connection error after it has been cached Mongo shuts down ?

I have created a mongodb native connection and saved it and then using findOne to query a document.
const Promise = require("bluebird");
const MongoClient = require('mongodb').MongoClient;
let mongoDB = undefined;
const getCollection = (collName) => {
if (mongoDB) {
return Promise.resolve(mongoDB.collection(collName));
} else {
return MongoClient.connect(EXT_CONFS.MONGODB_URL)
.then(db => {
mongoDB = db;
return Promise.resolve(mongoDB.collection(collName));
}).catch(e => {
console.error('Error in MongoDb connection');
});
}
};
const findOne = (collName, filter, options) => {
return getCollection(collName)
.then(collection => {
return collection.findOne(filter, options);
})
.then(doc => {
return Promise.resolve(doc);
}).catch(e => {
console.error(e);
return Promise.reject(e);
});
};
Now this all works fine, but if Mongo ShutsDown / Fails after db client is cached, There is no way to handle error. Error never goes to any catch handler :
console.error('Error in MongoDb connection');
or
console.error(e);
I even tried events :
mongoDB.on('connecting', function () {
console.log('connecting');
});
mongoDB.on('timeout', function (error) {
console.log('timeout!');
});
mongoDB.on('close', function (error) {
console.log('close!');
});
mongoDB.on('error', function (error) {
console.error('Error in MongoDb connection: ' + error);
});
mongoDB.on('connected', function () {
console.log('connected!');
});
mongoDB.on('connection', function () {
console.log('connected!');
});
mongoDB.on('connect', function () {
console.log('connected!');
});
mongoDB.once('open', function () {
console.log('connection open');
});
mongoDB.on('reconnected', function () {
console.log('reconnected');
});
mongoDB.on('disconnected', function () {
console.log('disconnected');
});
but no success still. Using NodeJS 4.5.0, MongoDB-Native driver 2.2.24
You should do something like console.error('Failed to connect to mongodb ',e); you are not outputting the error.
Also some events provide an additional parameter and you are outputting those either. In case of failing to connect to an mongodb server, your application should just notify you it's not the best approach to handle mongodb server start/restart from your application use daemons such as systemd or other process monitoring.
Some events are there to just notify the application that connection was lost or an reconnection is attempted, its up to you to handle what is going to be done when those events are emitted.
You can for example attempt to check mongodb status when an disconnect event is emitted an recreate connection object.
You could wrap the connect statement in a try-catch block.

Mongoose - Save doesnt work when am connecting to mongodb running in different server

When am trying to save a document in a collection or find the list of collections in mongodb running remotely doesnt work but works when am connecting to mongo db locally. save doesnt show if it throwed an error or it was successful.
Your help is appreciated.
below is my code
var mongoose = require('mongoose');
var dbName = 'OST';
var connectionString = 'mongodb://vm-1b98-f53f.nam.nsroot.net:32017/OST?ssl=true&sslverifycertificate=false';
mongoose.connect(connectionString);
mongoose.connection.on('error', function (err) {
console.log('Mongoose default connection error: ' + err);
});
mongoose.connection.on('connected', function () {
console.log('Mongoose default connection open to ' + connectionString);
var Region = require('./Region.js');
var r = new Region({ NodeID: 8687, RegionName: 'EMEA' });
r.save(function (err, regionObj) {
if (err) {
console.log('error');
throw err;
} else {
console.log('saved successfully:', regionObj);
}
});
mongoose.connection.db.collectionNames(function (error, names) {
if (error) {
console.log(error);
} else {
names.map(function (cname) {
console.log(cname.name);
});
}
});
});
process.on('SIGINT', function () {
mongoose.connection.close(function () {
process.exit(0);
});
});

Resources