Mongoose Connection

I read a quick guide from the Mongoose website and almost copied the code, but can't connect MongoDB using Node.js.

var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/test'); exports.test = function(req, res) { var db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error:')); console.log("h1"); db.once('open', function callback () { console.log("h"); }); res.render('test'); }; 

This is my code. The console prints only h1 , not h . Where am I wrong?

+17
source share
5 answers

When calling mongoose.connect it will establish a connection to the database.

However, you attach an event listener for open at a much later point in time (when the request is processed), which means that the connection is probably already active and the open event has already been raised (you just haven't listened to it yet).

You must change your code so that the event handler is as close as possible (in time) to the connection call:

 var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/test'); var db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error:')); db.once('open', function callback () { console.log("h"); }); exports.test = function(req,res) { res.render('test'); }; 
+23
source

The safest way to do this is to "listen for the connect event". That way, you don't care how long it takes for the database to provide you with a connection.

Once this is done, you must start the server. Also .. config.MONGOOSE is displayed through your application, so you only have one database connection.

If you want to use the mongoose connection, just require the configuration in your module and call config.Mongoose. Hope this helps someone!

Here is the code.

 var mongoURI; mongoose.connection.on("open", function(ref) { console.log("Connected to mongo server."); return start_up(); }); mongoose.connection.on("error", function(err) { console.log("Could not connect to mongo server!"); return console.log(err); }); mongoURI = "mongodb://localhost/dbanme"; config.MONGOOSE = mongoose.connect(mongoURI); 
+11
source

There was an error. Then I found out that mongod is not working for me and they are listening to connections. To do this, you just need to open another command line (cmd) and run mongod

+2
source

Mongoose's default connection logic has been deprecated since 4.11.0. It is recommended to use the new connection logic:

  • useMongoClient
  • local promise library

Here is an example from the npm module: mongoose-connect-db

 // Connection options const defaultOptions = { // Use native promises (in driver) promiseLibrary: global.Promise, useMongoClient: true, // Write concern (Journal Acknowledged) w: 1, j: true }; function connect (mongoose, dbURI, options = {}) { // Merge options with defaults const driverOptions = Object.assign(defaultOptions, options); // Use Promise from options (mongoose) mongoose.Promise = driverOptions.promiseLibrary; // Connect mongoose.connect(dbURI, driverOptions); // If the Node process ends, close the Mongoose connection process.on('SIGINT', () => { mongoose.connection.close(() => { process.exit(0); }); }); return mongoose.connection; } 
+1
source

A simple way to establish a connection:

 import mongoose from 'mongoose' mongoose.connect(<connection string>); mongoose.Promise = global.Promise; mongoose.connection.on("error", error => { console.log('Problem connection to the database'+error); }); 
0
source

Source: https://habr.com/ru/post/959267/


All Articles