I have server.js and db.js The db.js file interacts with my database using Mongoose and I use server.js to call functions from db.js :
var mongoose = require('mongoose'); mongoose.connect('', { useNewUrlParser: true }) var Schema = mongoose.Schema; module.exports = function () { var db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error:')); return db.once('open', function() { console.log("Connected to DB") var postschema = new Schema({ title: String, intro: String, body: String, author: String, timestamp: { type: Date, default: Date.now } }); var post = mongoose.model('post', postschema); return { newPost(title, intro, body, author) { var newpost = new post({ title: title, intro: intro, body: body, author: author }) }, getPostsAll() { post.find({}, function (err, res) { return (`Error:${err} Posts:${res}`) }) } } }) } And my server.js calls three functions from db.js :
var DB = require('./db.js') var db = DB() db.getPostsAll() db.newPost() I don't understand why I get this error :
connection error: { MongoNetworkError: connection 4 to black-test-shard-00-01-ewyaf.mongodb.net:27017 closed at TLSSocket.<anonymous> (E:\HTML\black-box\node_modules\mongodb-core\lib\connection\connection.js:276:9) at Object.onceWrapper (events.js:272:13) at TLSSocket.emit (events.js:185:15) at _handle.close (net.js:541:12) at TCP.done [as _onclose] (_tls_wrap.js:379:7) name: 'MongoNetworkError', errorLabels: [ 'TransientTransactionError' ], [Symbol(mongoErrorContextSymbol)]: {} } What am I doing wrong? I found an article but can't make anything of it.
