Mongoose 5.2.1收到MongoDB连接错误:MongoError:没有可用的mongos代理

时间:2018-07-06 23:40:30

标签: node.js mongoose

对于以下问题的任何帮助,我们深表感谢。在广泛研究以下内容但没有发现任何内容时,NodeJS MongoDB驱动程序似乎存在问题,或者对mongoose进行了更改,导致了以下错误:“ MongoError:没有可用的mongos代理”

作为示例,在使用以下模块时:

const mongoose = require("mongoose");

//Setup with native ES6 Promise Library
mongoose.Promise = Promise;

//Setup UUID type i.e. const { UUID } = mongoose.Types;
require("mongoose-uuid2")(mongoose);

//Extend mongoose object with uuid operations - uuid.v4 method i.e. mongoose.uuid.v4
const uuid = require("uuid/v4");

//Extend mongoose object - Setup bson type - i.e. mongoose.bson
const bson = require("bson");

//Extend mongoose object - Setup Url type i.e. - mongoose.SchemaTypes.Url
require("mongoose-type-url");

//Extend mongoose object - Setup extend_schema i.e. - mongoose.extend_schema();
const extend_schema = require("mongoose-extend-schema");

/*
 *Initializes new instance of mongoose, sets up instance, provides reference to instance
 *Wires up the models to the instance
 *Defaults to test database using uri string
 */
class MongooseSetupService {

  constructor(uri = "mongodb://localhost:27017/test", debug = false) {

    const db = mongoose.connection;

    mongoose.uuid = uuid;
    mongoose.bson = bson;
    mongoose.extend_schema = extend_schema;

    this._database_uri = uri;
    this._mongoose = mongoose;

    mongoose.connect(this._database_uri, { "useNewUrlParser": true });
    mongoose.set("debug", debug);
    MongooseSetupService.listen(db);
  }

  get mongoose () {
    return this._mongoose;
  }

  static listen(db) {
    db.on("error", (error) => {
      console.error(`MongoDB connection error: ${error}`);
    });

    db.on("open", () => {
      console.log("Connected to MongoDB");
    });

    db.on("connected", () => {
      console.log(`Mongoose default connection open to ${this._database_uri}`);
    });

    db.on("disconnected", () => {
      console.log("Mongoose default connection disconnected");
    });

    //If the Node process ends, close the Mongoose connection
    process.on("SIGINT", () => {
      db.close(() => {
        console.log("Mongoose default connection disconnected through app termination");
        process.exit(0);
      });
    });
  }
}

module.exports = MongooseSetupService;

如果我尝试食用它,例如:

const MongoSetupService = require('./test2')

const mongo_setup_service = new MongoSetupService(
    'mongodb://user:password@server-1.domain.com:27017,server-2.domain.com:27017,server-3.domain.com:27017/db?ssl=true&replicaSet=rs0', true 
);

我收到以下错误消息:

MongoError: no mongos proxy available

这是package.json依赖项

"dependencies": {
    "bson": "^3.0.0",
    "mongodb": "^3.1.1",
    "mongoose": "^5.2.1",
    "mongoose-extend-schema": "git+https://github.com/thxmike/mongoose-extend-schema.git",
    "mongoose-type-url": "^1.0.4",
    "mongoose-uuid2": "^2.1.0",
    "uuid": "^3.3.2"
  }

最近开始发生在最新版本的Mongoose(5.2.1)中 它使用的是NodeJS MongoDB驱动程序的3.1.1。 调试日志提供错误消息。 感谢您提供有关此事的帮助。


经过进一步调查,该选件似乎有问题 {useNewUrlParser:true}。由于连接提供了警告消息,因此添加了该消息。 如果删除此选项,问题将消失。 另外,如果我从连接字符串uri中删除&replicaSet = rs0,问题又回来了。有趣的是该驱动程序如何使用不同的设置。如果提供有关这些设置的更多信息,那就太好了。

0 个答案:

没有答案