Felipe Garcia-Diaz
Felipe Garcia-Diaz

Reputation: 119

nodeJS module mongoose is giving me errors

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?

Upvotes: 1

Views: 199

Answers (2)

foghost
foghost

Reputation: 238

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.

Upvotes: 0

Hiren S.
Hiren S.

Reputation: 2832

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);
        }
    });
});

Upvotes: 1

Related Questions