nodeJS module mongoose is giving me errors nodeJS module mongoose is giving me errors mongoose mongoose

nodeJS module mongoose is giving me errors


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,somongoose.connect("mongodb://localhost:27017/NEW_DB1");will execute manny times,this cause the error.