1

As said in the title the following program gives me that error:

const express = require ('express');
const app = express();
const mongoose = require('mongoose');
const { TypedRule } = require('tslint/lib/rules');
const Schema = mongoose.Schema;
const opciones = {useNewUrlParser:true,
  user:"user",
  pass:"usuario1",
  useUnifiedTopology: true};

// mongoose.Promise = global.Promise; 

const conn = mongoose.createConnection('mongodb://10.6.130.209:27017/app_user',opciones).then(()=>{
  console.log("Conexion a la base de dato correcta");

}).catch((e)=>{
    console.log("Error al conectar con la base de datos");
    console.log(e);
});

mongoose.set('useCreateIndex',true);
mongoose.set('useFindAndModify',false);

const ListSchema =  new Schema({
   ....
})
const Usuario = conn.model('usuarios',ListSchema);

I've tried with mongoose.model previously, but i need multiple connection to the DB Server, so i use createConnection. The Schema is done in a good way, i just simplified that.

Edit:

Deleting the model part:

const mongoose = require('mongoose');
const { TypedRule } = require('tslint/lib/rules');
const Schema = mongoose.Schema;
const opciones = {useNewUrlParser:true,
  user:"user",
  pass:"usuario1",
  useUnifiedTopology: true,
  useNewUrlParser:true
};

var conn = mongoose.createConnection('mongodb://10.6.129.31:27017/app_user',opciones).then(()=>{
  console.log("Conexion a la base de datos correcta");
}).catch((e)=>{
  console.log("Error al conectar con la base de datos");
  console.log(e);
});

mongoose.set('useCreateIndex',true);
mongoose.set('useFindAndModify',false);

The program crashes with the following error message:

MongooseServerSelectionError: connection timed out
....
  reason: TopologyDescription {
    type: 'Unknown',
    setName: null,
    maxSetVersion: null,
    maxElectionId: null,
    servers: Map { '10.6.129.31:27017' => [ServerDescription] },
    stale: false,
    compatible: true,
    compatibilityError: null,
    logicalSessionTimeoutMinutes: null,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    commonWireVersion: null
  }

Edit II:

It seems to be the mongo server, i used this:

$mongo mongodb://10.6.129.31:27017/app_user -u "user" -p "usuario1"

And Mongo says this :

MongoDB shell version v4.4.3
connecting to: mongodb://10.6.129.31:27017/app_user?compressors=disabled&gssapiServiceName=mongodb
Error: couldn't connect to server 10.6.129.31:27017, connection attempt failed: SocketException: Error connecting to 10.6.129.31:27017 :: caused by :: Connection timed out :
connect@src/mongo/shell/mongo.js:374:17
@(connect):2:6
exception: connect failed
exiting with code 1
k1k4ss0
  • 87
  • 10

1 Answers1

0

Like you see in this answer, try something like this:

var mongoose = require('mongoose')
var conn = mongoose.createConnection('mongodb://localhost/db1');
var conn2 = mongoose.createConnection('mongodb://localhost/db2');
var Schema = new mongoose.Schema({})
var model1 = conn.model('User', Schema);
var model2 = conn2.model('Item', Schema);
model1.find({}, function() {
   console.log("this will print out last");
});
model2.find({}, function() {
   console.log("this will print out first");
});

You can also check Mongoose documentation and try with a connection pool

  • ok, i will try but the problem should still remain ..., in a moment i will notify if it works – k1k4ss0 Feb 24 '21 at 18:49
  • Have you even tried with the connection pool? –  Feb 24 '21 at 19:11
  • yes, i've tried both, but wait because i'found a error. Let me edit – k1k4ss0 Feb 24 '21 at 19:22
  • Does it gives you any error or something similar so we can investigate? –  Feb 24 '21 at 19:23
  • I continue here the discussion because I haven't enough reputation to comment the original post. In your code, you wrote useNewUrlParser: true twice in your options, maybe it's because of this –  Feb 24 '21 at 20:02
  • Np. but as i said in `Edit II`, it seems to be the server because if i try with commandline it still fails.(i removed `useNewUriParser`, still the same) – k1k4ss0 Feb 24 '21 at 20:17