Nodejs - Difference between require('module') and require('module').db? - node.js

//module.js
exports.doA = function(callback) {
db.asyncConnect(/* connect options */, function(err, database) {
if(err == null) {
exports.db = database;
}
});
}
exports.db = null;
// test1.js
var mydb = require('module');
console.log(mydb);
// test2.js
var db = require('module').db;
console.log(db);
How is var mydb = require('module'); different from var mydb = require('module').db; ?
Update: Updating with the code and behavior I am observing
// file: db.js
exports.init = function (start_server){
MongoClient.connect(url, {
db: {
raw: true
},
server: {
poolSize: 5
}
},
function(err, database) {
exports.db = database;
if(err == null)
start_server();
}
);
}
exports.db = null;
.
// file: test.js
var mongodb = require('./db.js');
var db = require('./db.js').db;
console.log("MongoDb " + mongodb.db);
console.log("DB " + db);
Output:
MongoDb [object Object]
DB null
Q. The variable db is coming out to be null but mongodb.db is having values?
Even if I assign the value of mongodb.db in a variable, the value is coming out to be null.

var mydb = require('module');
This gets the entire module.
var mydb = require('module').db;
This gets the property db of the object that the module returns.
The downside to the latter part is that you can't access the "parent" object anymore, ie. whatever is returned from the .db call, will be what you get and nothing else.
Quick example:
//some_module.js
var SomeModule = {
db: function () {
console.log("hello");
}
};
module.exports = SomeModule;
//some_other_module.js
var SomeModule = require('./some_module.js');
console.log(SomeModule); // [Object object]
var SomeDB = require('./some_module.js').db;
console.log(SomeDB); // function () {}

It's basically different
var mydb = require('module');
That means every object that you export will be called/ imported. mydb.doA and mydb.db are available.
var db = require('module').db;
Only db object is called/ imported to your code. Different from first example, db it self is like mydb.db in the first example.

Related

Parameter obj to Document() must be an object when trying to convert array to mongoose document with redis

I have using redis to cache my queries. Its working fine with object but not when i get array. It gives me an error **"Parameter "obj" to Document() must be an object, got kids", **. It also happens with count query. Here is my code :
const mongoose = require("mongoose");
const redis = require("redis");
const util = require("util");
const client = redis.createClient(process.env.REDIS_URL);
client.hget = util.promisify(client.hget);
const exec = mongoose.Query.prototype.exec;
mongoose.Query.prototype.cache = async function (options = {}) {
this.useCache = true;
this.hashKey = JSON.stringify(options.key || "");
this.time = JSON.stringify(options.time || 36000);
return this;
};
mongoose.Query.prototype.exec = async function () {
if (!this.useCache) {
return exec.apply(this, arguments);
}
const key = JSON.stringify(
Object.assign({}, this.getQuery(), {
collection: this.mongooseCollection.name,
})
);
// client.flushdb(function (err, succeeded) {
// console.log(succeeded); // will be true if successfull
// });
const cacheValue = await client.hget(this.hashKey, key);
if (cacheValue) {
const doc = JSON.parse(cacheValue);
/*
this.model refers to the Class of the corresponding Mongoose Model of the query being executed, example: User,Blog
this function must return a Promise of Mongoose model objects due to the nature of the mongoose model object having other
functions attached once is created ( validate,set,get etc)
*/
console.log("Response from Redis");
console.log(doc);
console.log(Array.isArray(doc));
return Array.isArray(doc)
? doc.map((d) => new this.model(d))
: new this.model(doc);
}
//await the results of the query once executed, with any arguments that were passed on.
const result = await exec.apply(this, arguments);
client.hset(this.hashKey, key, JSON.stringify(result));
client.expire(this.hashKey, this.time);
console.log("Response from MongoDB");
return result;
};
module.exports = {
clearHash(hashKey) {
client.del(JSON.stringify(hashKey));
},
};
Data in redis - [ 'kids', 'men', 'women' ]
Query - const collectionType = await Product.find() .distinct("collectionType") .cache({ key: "COLLECTION_TYPE" });
can i anyone please tell me what i am doing wrong?
I have solved by directly returning the doc and its working fine. Not sure if it is the right way if i directly do return doc then sending data from redis only

Data does not display on first try only upon refresh

I am new to node, express and mongo so I need some guidance. I set up a mongo database with 3 objects. I created an application with a route so that http://localhost:3000/Employeeid will lead the page to display all the 3 objects in the database, which the page does. However for each subsequent refresh, the same data gets displayed multiple number of times based on the number of refresh. (i.e. For the first refresh, the data duplicates once. For the second refresh, the data duplicates twice.) Does anyone know what may be wrong?
var express = require('express')
var app = express()
var MongoClient = require('mongodb').MongoClient
var url = 'mongodb://localhost:27017'
var str = ''
app.route('/Employeeid').get(function(req, res) {
MongoClient.connect(url, { useNewUrlParser: true }, function(err, client) {
var db = client.db('EmployeeDB')
var cursor = db.collection('Employee').find()
cursor.forEach(function(item) {
if (item != null) {
str = str + ' Employee id&nbsp ' +
item.Employeeid + '</br>'
}
})
res.send(str)
client.close()
})
})
var server = app.listen(3000, function() {})
You are appending to str every time the /EmployeeId route is called. To fix this, move str inside the callback:
app.route('/Employeeid').get(function(req, res) {
MongoClient.connect(url, { useNewUrlParser: true }, function(err, client) {
var str = ''
var db = client.db('EmployeeDB')
var cursor = db.collection('Employee').find()
cursor.forEach(function(item) {
if (item != null) {
str = str + ' Employee id&nbsp ' +
item.Employeeid + '</br>'
}
})
res.send(str)
client.close()
})
})
Side note - you should reuse the Mongo connection instead of calling connect and close on every request.
The Reason why your getting duplicate results is you are not making str = null after sending the result. you are concatenating str for each request because str is a global variable. your code can be modified for performance as like this.
var express = require('express')
var app = express()
var MongoClient = require('mongodb').MongoClient
var url = 'mongodb://localhost:27017'
var str = ''
var db = null
MongoClient.connect(url, { useNewUrlParser: true }, function(err, client) {
if( err ) {
throw new Error( err );
}
db = client.db('EmployeeDB') // we are storing db reference in global variable db
});
app.route('/Employeeid').get(function(req, res) {
db.collection('Employee').find().toArray( function(err, cursor){
if( err ) {
res.send('');
throw new Error(err );
}
cursor.forEach(function(item) {
if (item != null) {
str = str + ' Employee id&nbsp ' +
item.Employeeid + '</br>'
}
}
res.send(str)
str = '';
})
})
})
var server = app.listen(3000, function() {})
If you don't move res.send() into callback function you may send result before getting data from db due to async nature of node.

Return the specific contents from a field of data in the result-set of a select query

I have select query that pulls a row of a data from a largeObject stored in a PostgreSQL table. The particular piece of data in the field is a html file.
I can output the name of the field of data into the console which appears as 16543 (for 16543kB).
So, my burning question is how I can return the actual contents (html) so that I can subsequently export it as one object and send it to the browser.
I am using node and express heres my source code so far:
var database = require('../database/postgresDB.js');
var pg = require('pg');
var html = {};
var connectionString = "postgres://dabladmin:dabldem#localhost:5432/dablpatient";
var client = new pg.Client(connectionString);
client.connect();
var query = client.query('SELECT * FROM htmlfiles WHERE id = 1', function(err, result){
console.log(JSON.stringify(result));
console.log(result.rows[0].htmlfile);
html = result.rows[0].htmlfile;
//return result.rows[0].htmlfile;
//console.dir(html);
});
module.exports = html;
This cannot be done directly. You need to export a function which will return the promise.
Following is an idea of how it can be done. Note: The code is not tested.
// htmlfile.model.js
const promise = require('bluebird'); // or any other Promise/A+ compatible library;
const initOptions = {
promiseLib: promise // overriding the default (ES6 Promise);
};
const pgp = require('pg-promise')(initOptions);
// Database connection details;
const cn = {
host: 'localhost', // 'localhost' is the default;
port: 5432, // 5432 is the default;
database: 'myDatabase',
user: 'myUser',
password: 'myPassword'
};
const db = pgp(cn); // database instance;
const getHtml = id => db.oneOrNone('SELECT * FROM htmlfiles WHERE id = $1', id);
module.exports = getHtml;
Inside some.controller.js:
const html_model = require('./htmlfile.model.js');
html_model.getHtml(1)
.then(data => {
if(data) {
// record found
res.send(data.htmlfile);
} else {
// record not found, do something else
}
})
.catch(error => {
// an error occurred
});

How to properly use Mongoose models with Node.js?

I'm trying to use mongoose to control my db logic and transactions. I already got Schema definitions and I'm exporting the models.
Howver when i try to use a model, it will fail witl a message like:
return mongoose.model('Report', reportSchema);
} has no method 'find'...
This is my Model export:
module.exports = (function() {
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var reportSchema = mongoose.Schema({
category: ObjectId,
authorName: String,
authorEmail: String,
text: String,
address: String,
coordinates: {
type: "Point",
coordinates: [Number,Number]
},
date: {
type: Date,
default: new Date()
},
comments: Array
});
return mongoose.model('Report', reportSchema);
});
And this is how my controller functions are coded using mongoose inside:
module.exports = (function() {
var mongoose = require('mongoose');
var Report = require('../models/Report');
var Category = require('../models/Category');
function _getReports (request,response,next) {
var take = request.query.take;
var skip = request.query.skip;
Report.find({}).limit(take).skip(skip).exec(function (err,reports) {
callback(err,reports,response);
});
}
function _getReport (request,response,next) {
var id = request.params.id;
Report.findById({_id: id}, function (err,report) {
callback(err,report);
});
}
function _createReport (request,response) {
var newReport = new Report();
newReport.text = request.body.text;
newReport.category = request.body.category;
newReport.author = request.session.userId;
newReport.save(function (err,savedReport) {
callback(err,savedReport._id,response);
});
}
function _updateReport (request,response) {
var id = request.params.id;
var update = request.body.report;
Report.findByIdAndUpdate(id, update, function (err,foundReport) {
callback(err,foundReport,response);
});
}
function _deleteReport (request,response) {
var id = request.params.id;
Report.findByIdAndRemove(id, function (err,foundReport) {
callback(err,foundReport,response);
});
}
function _getReports (request,response,next) {
var take = request.query.take;
var skip = request.query.skip;
Report.find({}).limit(take).skip(skip).exec(function (err,reports){
callback(err,reports,response);
});
}
function _getCategories (request,response) {
var take = request.query.take;
var skip = request.query.skip;
Report.find({}).limit(take).skip(skip).exec(function (err,reports) {
callback(err,reports,response);
});
}
function _getCategoryReports (argument) {
var _id = mongoose.Types.ObjectId(request.params.id);
Report.find({category:id},{category:false},function (err, foundReports) {
callback(err,foundReports,response);
});
}
function _createCategory (request,response) {
var newCategory = new Category();
newCategory.name = request.body.name;
newCategory.save(function (err,savedCategory) {
callback(err,savedCategory._id,response);
});
}
function _updateCategory (request,response) {
var id = request.params.id;
var update = request.body.category;
Category.findByIdAndUpdate(id, update, function (err,foundCategory) {
callback(err,foundCategory,response);
});
}
function _deleteCategory (request,response) {
var id = request.params.id;
Category.findByIdAndRemove(id, function (err,foundCategory) {
callback(err,foundCategory,response);
});
}
function callback (err,object,response) {
if (err)
response.status(500).send(JSON.stringify(err));
response.send(JSON.stringify(object));
}
var apiController = {
getReports: _getReports,
getReport: _getReport,
createReport: _createReport,
updateReport: _updateReport,
deleteReport: _deleteReport,
getCategories: _getCategories,
getCategoryReports: _getCategoryReports,
createCategory: _createCategory,
updateCategory: _updateCategory
}
return apiController;
})();
Before this, a mongoose connection is ensured:
var connectToMongoose = function (mongoose,app) {
var connect = function () {
var options = { server: { socketOptions: { keepAlive: 1 } } };
mongoose.connect( 'mongodb://localhost/data4', options);
}
mongoose.connection.on('connected', function () {
console.log('Connected to db');
app.listen(32884, function() {
console.log("Listening at \"data4 port\" #:32884");
});
})
mongoose.connection.on('error', function (err) {
console.log(err);
});
mongoose.connection.on('disconnected', function () {
console.log('Disconnected from db, attempting to connect again...');
app.close();
connect();
});
connect();
};
module.exports = connectToMongoose;
Which is invoked by require('./db/mongoose-connect.js')(mongoose,app);
What am I doing wrong?
There are a couple issues here that I caught off the bat.
First off, I don't see a mongoose.connect() line that explicitly connects your mongoose ODM to a mongo server+database. An example would be:
var mongoose = require( 'mongoose' ),
Schema = mongo.Schema,
ObjectId = mongo.Schema.ObjectId;
mongoose.connect( 'mongodb://localhost/db_name' );
Your schema export looks fine. But you're using an anonymous function as your export. Since you're doing that, your require statement needs to change a little:
var Report = require('../models/Report')();
var Category = require('../models/Category')();
Notice the () at the end of the require statements. You need to execute the function that you're defining as your model file's module.export.
EDIT: I see that you added your mongoose connect code. At this point, executing the module.exports function that you assign in the model file should allow your mongoose models to function as intended.
When you export a function;
// file: A.js
module.exports = function () {
//some logic
};
And you want to use it on another file, when you require the A file, you are importing a function and in order to use that function, you need to to invoke it.
// file: B.js
var A = require('./A.js');
A();
so your model is exporting a function
module.exports = (function() {
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
// ..
// some code
// ..
return mongoose.model('Report', reportSchema);
});
and when you are importing your model from your controller, you need to execute your imported function so that your Report variable contains the model created:
module.exports = (function() {
var mongoose = require('mongoose');
var Report = require('../models/Report') ();
I have created a gist of how you could write your code using modules without using IIFE.
https://gist.github.com/wilsonbalderrama/d5484f3f530899f101dc
actually if you download all those files on a folder and run:
$ sudo npm install
$ mocha
You could see that all the tests created for the controller are passing.
In addition you don't need to use IIFE in Node.JS since when you are creating a module because you already have an isolated scope in Node.JS using modules.
// IIFE
module.exports = (function() {
var apiController = {
getReport: function () {}
}
return apiController;
})();
In Node.JS you can export a object,
// GOOD WAY
module.exports = {
getReport: function () {}
};

Empty reply from server error in CURL with node.js, express and mongoDB

I am getting the error "Empty reply from server" In CURL, when I try to run "http://localhost:3000/light"
var express = require('express'),
values = require('./routes/values');
var app = express();
app.get('/light', values.findAll);
app.listen(3000);
console.log('Listening on port 3000...');
and my values.js is
var mongo = require('mongodb');
var Server = mongo.Server,
Db = mongo.Db,
BSON = mongo.BSONPure;
var server = new Server('localhost', 27017, {auto_reconnect: true});
db = new Db('sensordb', server);
db.open(function(err, db) {
if(!err) {
console.log("Connected to 'sensordb' database");
db.collection('values', {safe:true}, function(err, collection) {
if (err) {
console.log("The 'values' collection doesn't exist. Creating it with sample data...");
populateDB();
}
});
}
});
exports.findAll = function(req, res) {
db.collection('values', function(err, collection) {
collection.find().toArray(function(err, items) {
res.send(items);
});
});
};
var populateDB = function() {
var values = [
{
value: "10",
date: "121212",
time: "1214"
},
{
value: "12",
date: "121212",
time: "1224"
}];
db.collection('values', function(err, collection) {
collection.insert(values, {safe:true}, function(err, result) {});
});
};
Well basically in the above code, I create a database and if the database is empty, I try to populate it. When I run the server code on my terminal I also get something like:
= Please ensure that you set the default write concern for the database by setting =
= one of the options =
= =
= w: (value of > -1 or the string 'majority'), where < 1 means =
= no write acknowlegement =
= journal: true/false, wait for flush to journal before acknowlegement =
= fsync: true/false, wait for flush to file system before acknowlegement =
= =
= For backward compatibility safe is still supported and =
= allows values of [true | false | {j:true} | {w:n, wtimeout:n} | {fsync:true}] =
= the default value is false which means the driver receives does not =
= return the information of the success/error of the insert/update/remove =
= =
= ex: new Db(new Server('localhost', 27017), {safe:false}) =
= =
= http://www.mongodb.org/display/DOCS/getLastError+Command =
= =
= The default of no acknowlegement will change in the very near future =
= =
= This message will disappear when the default safe is set on the driver Db =
========================================================================================
Is the above error causing some problem? If so, what? if not, what is responsible?
Any help would be really appreciated.
It seems that your populateDB function uses the global db variable, while the opened db is a different variable, given as parameter to the callback you provide when opening the database.
This is caused since you have two different variables called db, each visible in a different scope and refers to a different object. To make the picture clearer, I added a parameter to your populateDB function, and created db1, db2 callback parameters. The modifications below should help:
db.open(function(err, db1) {
...
populateDB(db1);
...
var populateDB = function(db2) {
...
db2.collection('values', function(err, collection) {
collection.insert(values, {safe:true}, function(err, result) {});
});
};

Resources