I am trying to connect to a Mongo app from my Node application using Mongoose. I have done this before on the same machine, but this time I am having a connection problem.
import { connect } from 'mongoose';
export class ConnectedModel {
protected async connect(): Promise<void>() {
const connectionString = 'mongodb://my-user-name:my-password@localhost/my-database';
await connect(connectionString);
}
}
This throws an error:
/my-project/node_modules/mongoose/lib/connection.js:807
const serverSelectionError = new ServerSelectionError();
^ MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
at NativeConnection.Connection.openUri (/my-project/node_modules/mongoose/lib/connection.js:807:32)
// many lines of error stack
at UserModel.connect (/my-project/bin/www/models/connected.model.js:6:38)
I thought the problem must be something to do with my credentials but if I use mongo "mongodb://my-user-name:my-password@localhost/my-database"
from the command line it opens up the shell as the right user in the right database.
The user belongs to the database I am attempting to use, so I don't think this is an authSource
problem and changing authSource has not had any effect so none of the solutions on this previous question are working for me.
Why does this connection string work from the command line but not through Mongoose? What do I need to change in order to be able to open the connection?