nodeJS module mongoose is giving me errors - javascript

When i run my code i get an error
What i'm trying to do is when someone logs on to my site it logs the IP and other data into a database. it seems to work but then i get this error and it exits out of my app
{ [Error: Trying to open unclosed connection.] state: 1 }
Connection to database has been established
/home/azura/node_modules/mongoose/lib/index.js:343
throw new mongoose.Error.OverwriteModelError(name);
^
OverwriteModelError: Cannot overwrite `dataType` model once compiled.
at Mongoose.model (/home/azura/node_modules/mongoose/lib/index.js:343:13)
at Namespace.<anonymous> (/home/azura/Desktop/dbWrite.js:19:37)
at Namespace.EventEmitter.emit (events.js:95:17)
at Namespace.emit (/home/azura/node_modules/socket.io/lib/namespace.js:205:10)
at /home/azura/node_modules/socket.io/lib/namespace.js:172:14
at process._tickCallback (node.js:415:13)
The code that im using is:
var mongoose = require("mongoose");
var express = require("express");
var app = express();
var http = require("http").Server(app);
var io = require("socket.io")(http);
app.get("/", function (req, res) {
res.sendFile(__dirname + "/index.html");
});
io.on("connection", function (socket) {
var ip = socket.request.socket.remoteAddress;
var dataBase = mongoose.connection;
mongoose.connect("mongodb://localhost:27017/NEW_DB1");
dataBase.on("error", console.error);
console.log("Connection to database has been established");
var collectedData = new mongoose.Schema({
ipAddress: String,
time: Number
});
var collectionOfData = mongoose.model("dataType", collectedData);
var Maindata = new collectionOfData({
ipAddress: ip,
time: 100000000000000000
});
Maindata.save(function (err, Maindata) {
if (err) {
return console.error(err);
} else {
console.dir(Maindata);
}
});
});
http.listen(10203, function () {
console.log("Server is up");
});
the index.html file has nothing important on it.
I'm just wondering why i'm getting this error.
what can i do to fix it?

Put this code out of connection scope. No Need to create Schema every type there is new connection event.
mongoose.connect("mongodb://localhost:27017/NEW_DB1");
dataBase.on("error", console.error);
console.log("Connection to database has been established");
var collectedData = new mongoose.Schema({
ipAddress: String,
time: Number
});
var collectionOfData = mongoose.model("dataType", collectedData);
io.on("connection", function (socket) {
var ip = socket.request.socket.remoteAddress;
var dataBase = mongoose.connection;
var Maindata = new collectionOfData({
ipAddress: ip,
time: 100000000000000000
});
Maindata.save(function (err, Maindata) {
if (err) {
return console.error(err);
} else {
console.dir(Maindata);
}
});
});

every time a connection come in then the "connection" event will be emit,so
mongoose.connect("mongodb://localhost:27017/NEW_DB1");
will execute manny times,this cause the error.

Related

I am having trouble connecting my app to Mongo Atlas

I am following the documentation and based off what I read I am doing it right. I am connecting to my Mongo Atlas server. The server connects and I am able to connect to the DB and the Collection. Yet the DB and the Collection are not being passed to the db object.
I have tried console logging the values and refactored my logic and yet still no solution.
// MongoDB Connection Setup
let db = {};
let MongoClient = require("mongodb").MongoClient;
let uri = process.env.MONGODB_CONNECT_URL;
let client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
assert.strictEqual(null, err);
console.log('Connected Successfully to MongoDB!');
db.client = client.db("cosmosdb");
db.collection = client.db('cosmosdb').collection('cosmos');
console.log("Database Values: ", db) // This actually returns values
return db;
});
console.log('Database: ', db); // Not returning values
app.set('port', process.env.PORT || 3000);
let server = app.listen(app.get('port'), () => {
console.log(`Express server listening on port: `, server.address().port)
});
server.db = db;
When I console.log db I am expecting to see
Database: {
client: // values
collection: // values
}
yet this is what I am getting back
Database: {}
EDITED
Is your uri assigned like below? (mongodb+srv)
let uri = `mongodb+srv://${dbUser}:${dbPwd}#${dbHost}/test?retryWrites=true`;
let client = new MongoClient(uri, { useNewUrlParser: true });
There is a parameter you are missing on the connect() call, you have "err", but it should be (err, client). So for me it looks as follows:
var db = {};
var MongoClient = require('mongodb').MongoClient;
//Use connect method to connect to the Server
MongoClient.connect(process.env.MONGODB_CONNECT_URL, { useNewUrlParser: true }, function (err, client) {
assert.equal(null, err);
db.client = client;
db.collection = client.db('newswatcherdb').collection('newswatcher');
console.log("Connected to MongoDB server");
});

Connecting Multiple Mongo DBs in a Node.js Project

I am trying to connect multiple MongoDB databases into a single Node.js project. Here is my current structure and issue at hand.
Node Version: v6.12.1
Express.js Version: 4.16.2
Mongoose Version: 4.13.6
Current Structure:
primaryDB.js
var mongoose = require('mongoose');
var configDB = require('./database.js');
//Connect to MongoDB via Mongoose
mongoose.Promise = require('bluebird');
//mongoose.Promise = global.Promise;
mongoose.connect(configDB.url, { useMongoClient: true });
//Check for successful DB connection
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("Primary DB Successfully Connected..");
});
module.exports = mongoose;
secondaryDB.js
var mongoose = require('mongoose');
mongoose.connect('mongodb://mongodb_address_goes_here:27017/db_name', { useMongoClient: true });
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("Secondary DB Successfully Connected..");
});
module.exports = mongoose;
Then each DB connection gets imported respectively into their schema files, from which the schema files have module exports.
Issue at hand
When I run my application it starts fine and connects to both DB's successfully however I believe that mongoose is either getting overwritten or something because I might be able to do a findOne() command on primary but secondary fails or vice versa.
Example:
var async = require('async');
var primaryModel = require('../../../models/general/primary');
var SecondaryModel = require('../../../models/general/secondary');
function getInfo() {
async.waterfall([
getPrimaryName,
getSecondaryName
], function (err, info) {
});
};
function getPrimaryName(callback){
Primary.findOne({}, function (err, primaryInfo){
if (err) {
console.log("Error" + err);
}
console.log('Primary info is : ' + primaryInfo);
callback(null,primaryInfo);
});
}
function getSecondaryName(primaryInfo, callback) {
console.log(primaryInfo); //Make sure its being passed
Secondary.findOne({}, function (err, secondaryInfo) {
if (err) {
console.log("Error" + err);
}
console.log('Secondary Info is : ' + secondaryInfo);
callback(null, secondaryInfo);
});
}
The problem with above is I might get data back from the call to Primary but not Secondary. Which again I believe is from something being overridden .
Any help appreciated. Sorry about the verbosity.
use mongoose.createConnection to create your connections
so
const conn1 = mongoose.createConnection('first server options')
const conn2 = mongoose.createConnection('second server options')
read more here
http://mongoosejs.com/docs/connections.html#multiple_connections

Error: collection name must be a String in node js

I am trying to run following code on server
var express = require("express"),
app = express(),
bodyParser = require('body-parser'),
errorHandler = require('errorhandler'),
methodOverride = require('method-override'),
hostname = process.env.HOSTNAME || 'localhost',
port = parseInt(process.env.PORT, 10) || 4004,
publicDir = process.argv[2] || __dirname + '/public';
var exec = require('child_process').exec;
var fs = require('fs');
var mongodb = require('mongodb'),
serverdb = new mongodb.Server('127.0.0.1', 27017, {}),
dbName = new mongodb.Db('prisync', serverdb, {});
var url = "urlinfo";
//Show homepage
app.get("/", function (req, res) {
//res.redirect("/index.html");
console.log("shubham ");
dbName.open(function (error, client){
var collection = new mongodb.Collection(client , url); //line 24 ====
collection.find().limit(20).toArray(function (err, dataObjArr){
var data = '';
var dataArr = [];
var i = dataObjArr.length;
//check for error
if(err){return res.end('error!'+err);}
//Data
if(dataObjArr){
while(i--){
dataArr[i] = dataObjArr[i]._id;
}
data = dataArr.join(' ');
res.render('/index.html',{ returnedData : data });
}else{
res.end();
}
});
});
});
app.get("/search", function (req, res){
console.log("shubham batra");
var pro_name = req.query.name;
var pro_code = req.query.code;
var pro_category = req.query.category;
var pro_brand = req.query.brand;
pro_name = pro_name+"";
pro_code = pro_code+"";
pro_brand = pro_brand+"";
pro_category = pro_category+"";
MongoClient.connect('mongodb://127.0.0.1:27017/prisync', function(err, db) {
if (err) throw err;
console.log("Connected to Database");
var documen = {name:pro_name, code:pro_code , category:pro_category, brand:pro_brand };
//documen = JSON.stringify(documen);
//insert record
db.collection('urlinfo').insert(documen, function(err, records) {
if (err) throw err;
});
res.redirect("/index.html");
});
});
//Search page
app.use(methodOverride());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static(publicDir));
app.use(errorHandler({
dumpExceptions: true,
showStack: true
}));
console.log("Server showing %s listening at http://%s:%s", publicDir, hostname, port);
app.listen(port);
but it gives following error while try to load localhost:4004
Server showing /home/shubham/Music/pricesync/server/public listening at http://localhost:4004
shubham
/home/shubham/node_modules/mongodb/lib/server.js:274
process.nextTick(function() { throw err; })
^
Error: collection name must be a String
at Error (<anonymous>)
at checkCollectionName (/home/shubham/node_modules/mongodb/lib/utils.js:69:11)
at new Collection (/home/shubham/node_modules/mongodb/lib/collection.js:57:3)
at /home/shubham/Music/pricesync/server/server.js:24:24
at /home/shubham/node_modules/mongodb/lib/db.js:221:5
at connectHandler (/home/shubham/node_modules/mongodb/lib/server.js:272:7)
at g (events.js:180:16)
at emit (events.js:95:17)
at /home/shubham/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:399:23
at /home/shubham/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:756:13
I had the same error, for me it was caused by my timestamp being incorrectly placed.
INCORRECT CODE:
}),{timestamps:true});
CORRECTED CODE:
var mongoose = require('mongoose');
var ObjectId = mongoose.Schema.Types.ObjectId;
var User = mongoose.model('User', new mongoose.Schema({
email:{type:String, required:true},
password:{type:String, required:true},
listings:[{type:ObjectId,ref:"Listings"}]
},{timestamps:true}));
Assuming the collection name is in 'url' varialbe (i.e. 'urlinfo'), the right way to instantiate the collection would be:
var collection = dbName.collection(url); //line 24
If you look at documentation, it states specifically that Collection class should not be instantiated directly : https://mongodb.github.io/node-mongodb-native/api-generated/collection.html
I got stuck at the same situation, while solving the exercise from Guillermos Smashing Node.JS book...
I followed Richard Andreus directions and after few attempts, succesfully established the server.
I newly defined
var Db = require('mongodb').Db,
Server = require('mongodb').Server;
instead of existing variable definitions.
As next I've defined the variable db.
var db = new Db('test', new Server('localhost', 27017));
Then open the connection to db, or in your case dbName.
db.open(function(err, db) {
var collection = db.collection(url);
// in my case I defined app.users instead of collection
NOTE: Don't forget your definition at dbName

can't create a new directory using mongoose and express

Like the title entails.
I'm trying to make an application that when i put in certain info, it creates a link using mongoose _id. and express's app.get what i don't get is that to be able to join that directory i have to reload the whole server, which for the users and my sake a i don't want to do.
var mongoose = require("mongoose");
var express = require("express");
var app = express();
var http = require("http").Server(app);
var io = require("socket.io")(http);
var router = express.Router();
app.get("/", function (req, res) {
var ip = req.connection.remoteAddress;
res.sendFile(__dirname + "/index.html");
});
mongoose.connect("mongodb://localhost:27017/NEW_DB1");
console.log("Connection to database has been established");
var collectedData = new mongoose.Schema({
ipAddress: String,
name: {
type: String,
unique: false
}
});
var collectionOfData = mongoose.model("dataType", collectedData);
io.on("connection", function (socket) {
socket.on("name", function (e) {
var ip = socket.request.socket.remoteAddress;
var dataBase = mongoose.connection;
var Maindata = new collectionOfData({
ipAddress: ip,
name: e
});
Maindata.save(function (err, Maindata) {
if (err) {
return console.error(err);
} else {
console.dir(Maindata);
}
});
});
});
app.get("/mix", function (req, res) {
collectionOfData.find(function (err, data) {
res.send(data);
});
});
collectionOfData.find(function (err, data) {
data.forEach(function (uniqueURL) {
app.get("/" + uniqueURL._id, function (req, res) {
res.send("<h1>Hello " + uniqueURL.ipAddress + "</h1><p>" + uniqueURL.name + "</p>");
});
});
});
http.listen(10203, function () {
console.log("Server is up");
});
So what i'm trying to do is make it so i don't have to reload the whole server, and i'm able to just join the created directory when it's done being loaded.
figured i should put a quick example:
localhost:10203/55c2b2f39e09aeed245f2996
is a link a user just created the long
55c2b2f39e09aeed245f2996
is the effect of the _id, but when the user try's to connect to that site it won't work until i reload the server and obviously i'd like to avoid that haha.
I have a index.html file, but all that has is a socket.emit that sends "name" to the server
app.get("/", function (req, res) {
var ip = req.connection.remoteAddress;
res.sendFile(__dirname + "/index.html");
});
app.get('/:uniqueURL', function(req, res){
var id = req.params.uniqueURL;
res.send("Your requested id : " + id);
})
Try to use this above.
You are creating fix get path inside collectionData.find. That is the problem. So each time you have to reload the server by restarting.

node js server: issue passing a variable to a function

I am building my first node.js server to perform a mysql query and return results from the database. Everything works but now I cannot find the right way to pass a value from the url (query section) to the function that performs the query (the PollingLoop function). No problems to retrieve the url and to get the parameter in the handler function but to move it to pollingLoop I have tried almost all I know about javascript (not enough I see). This is my code now that fails to run because of the reference error in pollingLoop for hwkey that is not defined.
var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
url = require('url'),
fs = require('fs'),
mysql = require('mysql'),
connectionsArray = [],
connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'flipper',
database: 'oclock',
port: 3306
}),
POLLING_INTERVAL = 3000,
pollingTimer;
// If there is an error connecting to the database
connection.connect(function(err) {
// connected! (unless `err` is set)
if (err) {
console.log(err);
}
});
// creating the server ( localhost:8000 )
app.listen(8000);
function handler(req, res) {
console.log("INCOMING REQUEST: "+req.method+" "+req.url);
req.parsed_url = url.parse(req.url, true);
var getp = req.parsed_url.query;
var hwkey = getp.hk;
console.log(hwkey);
fs.readFile(__dirname + '/client.html', function(err, data) {
if (err) {
console.log(err);
res.writeHead(500);
return res.end('Error loading client.html');
}
res.writeHead(200);
res.end(data);
});
}
function pollingLoop(){
// Doing the database query
var query = connection.query('SELECT max(id), testo, created_by FROM flashmsgs WHERE hwk="'+hwkey+'"'),
//var query = connection.query('SELECT max(id), testo, created_by FROM flashmsgs'),
flashmsgs = []; // this array will contain the result of our db query
// setting the query listeners
query
.on('error', function(err) {
// Handle error, and 'end' event will be emitted after this as well
console.log(err);
updateSockets(err);
})
.on('result', function(flashmsg) {
// it fills our array looping on each user row inside the db
flashmsgs.push(flashmsg);
})
.on('end', function() {
// loop on itself only if there are sockets still connected
if (connectionsArray.length) {
pollingTimer = setTimeout(pollingLoop, POLLING_INTERVAL);
updateSockets({
flashmsgs: flashmsgs
});
} else {
console.log('The server timer was stopped because there are no more socket connections on the app')
}
});
};
// creating a new websocket to keep the content updated without any AJAX request
io.sockets.on('connection', function(socket) {
console.log('Number of connections:' + connectionsArray.length);
// starting the loop only if at least there is one user connected
if (!connectionsArray.length) {
pollingLoop();
}
socket.on('disconnect', function() {
var socketIndex = connectionsArray.indexOf(socket);
console.log('socketID = %s got disconnected', socketIndex);
if (~socketIndex) {
connectionsArray.splice(socketIndex, 1);
}
});
console.log('A new socket is connected!');
connectionsArray.push(socket);
});
var updateSockets = function(data) {
// adding the time of the last update
data.time = new Date();
console.log('Pushing new data to the clients connected ( connections amount = %s ) - %s', connectionsArray.length , data.time);
// sending new data to all the sockets connected
connectionsArray.forEach(function(tmpSocket) {
tmpSocket.volatile.emit('notification', data);
});
};
console.log('Please use your browser to navigate to http://localhost:8000');
just bring on hwkey variable out from handler
var hwkey;
function handler(req, res) { hwkey = ... }
pollingLoop(){console.log(hwkey);}

Categories