Answer the question
In order to leave comments, you need to log in
How to properly organize access to mongo db through socket.io?
I have a small nodejs and express.js page. The client communicates with the server through socket.io.
client.on('addLocation', function (location) {
var locObj = JSON.parse(location);
var db = mongoose.connection;
db.on('error', function (error) {
console.log(error);
})
db.once('open', function () {
var collection = mongoose.model('esspunkts', essSchema);
var dbEsspunkt = new collection({
name: locObj.name,
rating: {
value: 0,
rated: 0
},
addrr: {
str: locObj.addr.str,
plz: locObj.addr.plz,
tel: locObj.addr.tel,
mapurl: "http://maps.google.com/?q=" + locObj.addr.plz + " " + locObj.addr.ort + " , " + locObj.addr.str,
menulink: locObj.menuurl
}
});
dbEsspunkt.save(function (err, dbObj) {
if (err) {
console.log(err);
} else {
console.log("save successfull ", dbObj);
mongoose.connection.close();
}
})
})
mongoose.connect(mongostr);
})
Answer the question
In order to leave comments, you need to log in
You need to open it once.
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect("localhost:27017", function(err) {
if (err) {
console.log('MongoDB Connection Error:', err);
process.exit(1);
}
});
const mongoose = require('mongoose');
let dbEsspunkt = new mongoose.Schema({
data: { type: any},
}, { timestamps: true });
dbEsspunkt.index(); // если нужно
var Esspunkt = mongoose.model('Esspunkt', dbEsspunkt);
module.exports = Esspunkt;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question