【问题标题】:Sails fails to load when using remote Mongo DB使用远程 Mongo DB 时,Sails 无法加载
【发布时间】:2014-04-01 01:07:49
【问题描述】:

我的 Sail 应用程序在连接到我的本地 mongo 数据库时运行,但是当我尝试使用 Modulus 托管的数据库进行连接时,它无法正确加载。

我已正确设置连接并通过 IDE 对其进行了测试

connections.js:
    // MONGO DB reference for the database
    cogspeed: {
    adapter   : 'sails-mongo',
    //host      : 'localhost',
    host      : 'novus.modulusmongo.net',
    port      : 27017,
    user      : '*********',
    password  : '***********',
    database  : '**********'
  },

我得到的错误是:

C:\Projects\gmm\cogspeed>sails lift

info: Starting app...

error: A hook (`orm`) failed to load!
error: Error: failed to connect to [localhost:27017]
    at null.<anonymous> (C:\Projects\gmm\cogspeed\node_modules\sails-mongo\node_modules\mongodb\lib\mongodb\conne
ction\server.js:553:74)
    at EventEmitter.emit (events.js:106:17)
    at null.<anonymous> (C:\Projects\gmm\cogspeed\node_modules\sails-mongo\node_modules\mongodb\lib\mongodb\conne
ction\connection_pool.js:140:15)
    at EventEmitter.emit (events.js:98:17)
    at Socket.<anonymous> (C:\Projects\gmm\cogspeed\node_modules\sails-mongo\node_modules\mongodb\lib\mongodb\con
nection\connection.js:512:10)
    at Socket.EventEmitter.emit (events.js:95:17)
    at net.js:441:14
    at process._tickDomainCallback (node.js:459:13)

为什么 Sails 还在查看 localhost?

提前致谢!

【问题讨论】:

  • 您的/config/models.js 文件是什么样的?
  • /config/models.js 是:module.exports.models = { // 您应用的默认连接。 // 即您的应用程序连接之一的名称(请参阅config/connections.js) // //(默认为 localDiskDb)连接:'cogspeed-mongo' };
  • 罪魁祸首原来是 /config/local.js 文件有一个指向本地主机的 mongo 适配器。

标签: mongodb sails.js


【解决方案1】:

我遇到了同样的问题。事实证明,在connections.js 中使用相同的适配器 有多个连接会导致此问题。我在航行 0.10.5

【讨论】:

  • 是的。我刚刚评论了localDiskDb 并留下了我的someMongodbServer,它起作用了。
  • 这个问题还在 Sailsjs v0.11 中。
【解决方案2】:

确认您的密码不包含“@”字符。由于 node-mongodb-native 的当前实现,它会弄乱你的连接字符串。

【讨论】:

    【解决方案3】:

    /config/local.js 文件仍然有一个指向 localhost 的 mongo 适配器。

    【讨论】:

      【解决方案4】:

      Sails Still (v 0.11.0) 在 /config/connections.js 配置文件中存在多个相同类型的适配器的问题。在 Modulus 上部署后,我遇到了同样的问题,支持无法帮助我。我花了几个小时试图调试它......但解决方法非常简单。不要将 db 连接参数放入 connections.js,而是将它们直接放入特定于环境的配置文件中,如下所示:

      // config/env/development.js
      module.exports = {
      
        connections : {
             mongo: {
                adapter: 'sails-mongo',
                host: 'localhost',
                port: 27017,
                database: 'your-db-name'
            },
        },
        // your other dev configs
      };
      

      用于生产:

      // config/env/development.js
      module.exports = {
      
        connections : {
             mongo: {
                adapter: 'sails-mongo',
                host: 'your-remote-mongo-host',
                port: 27017,
                user: 'your-remote-user',
                password: 'your-remote-db-password',
                database: 'your-remote-db-name'
            },
        },
        // your other dev configs
      };
      

      你的 /config/models.js 看起来像这样:

      module.exports = {
      
        connection : 'mongo',
      
        // your other model configs
      };
      

      PS:这个问题已经反馈给sails.js团队https://github.com/balderdashy/sails/issues/2480

      【讨论】:

        【解决方案5】:

        如果错误提示

        error: Error: failed to connect to [localhost:27017]
        

        那么,Mongo 没有运行。你可以用命令启动它

        mongod
        

        愚蠢的错误,但也会发生......


        如果错误提示错误:MongoError: auth fails,那么也许这个解决方案可以为您工作:https://stackoverflow.com/a/29335121/2615737

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-10-08
          • 1970-01-01
          • 2014-10-10
          • 2015-04-27
          • 2020-02-04
          • 2019-03-30
          • 1970-01-01
          • 2017-03-17
          相关资源
          最近更新 更多