【问题标题】:Sails.js "Could not tear down ORM hook" error with sails-mongoSails-mongo 的 Sails.js“无法拆除 ORM 钩子”错误
【发布时间】:2018-07-03 07:20:47
【问题描述】:

我最近在笔记本电脑上将 Sails 从 0.12 更新到了 1.0.2,并开始了一个新项目。我正在尝试使用sails-mongo 连接到MongoDB 数据库,但出现以下错误:

my-app_1  | error: A hook (`orm`) failed to load!
my-app_1  | error: Could not tear down the ORM hook.  Error details: Error: Consistency violation: Attempting to tear down a datastore (`default`) which is not currently registered with this adapter.  This is usually due to a race condition in userland code (e.g. attempting to tear down the same ORM instance more than once), or it could be due to a bug in this adapter.  (If you get stumped, reach out at http://sailsjs.com/support.)
my-app_1  |     at Object.teardown (/usr/src/app/node_modules/sails-mongo/lib/index.js:390:19)
my-app_1  |     at /usr/src/app/node_modules/waterline/lib/waterline.js:758:27
my-app_1  |     at /usr/src/app/node_modules/async/dist/async.js:3047:20
my-app_1  |     at eachOfArrayLike (/usr/src/app/node_modules/async/dist/async.js:1002:13)
my-app_1  |     at eachOf (/usr/src/app/node_modules/async/dist/async.js:1052:9)
my-app_1  |     at Object.eachLimit (/usr/src/app/node_modules/async/dist/async.js:3111:7)
my-app_1  |     at Object.teardown (/usr/src/app/node_modules/waterline/lib/waterline.js:742:11)
my-app_1  |     at Hook.teardown (/usr/src/app/node_modules/sails-hook-orm/index.js:246:30)
my-app_1  |     at Sails.wrapper (/usr/src/app/node_modules/@sailshq/lodash/lib/index.js:3275:19)
my-app_1  |     at Object.onceWrapper (events.js:313:30)
my-app_1  |     at emitNone (events.js:106:13)
my-app_1  |     at Sails.emit (events.js:208:7)
my-app_1  |     at Sails.emitter.emit (/usr/src/app/node_modules/sails/lib/app/private/after.js:56:26)
my-app_1  |     at /usr/src/app/node_modules/sails/lib/app/lower.js:67:11
my-app_1  |     at beforeShutdown (/usr/src/app/node_modules/sails/lib/app/lower.js:45:12)
my-app_1  |     at Sails.lower (/usr/src/app/node_modules/sails/lib/app/lower.js:49:3)
my-app_1  | error: Failed to lift app: Error: Consistency violation: Unexpected error creating db connection manager:
my-app_1  | ```
my-app_1  | ImplementationError: Internal error occurred while running `createManager`.  Got non-Error: { MongoError: failed to connect to server [mongodb:27017] on first connect [MongoError: getaddrinfo ENOTFOUND mongodb mongodb:27017]
my-app_1  |     at Pool.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/topologies/server.js:327:35)
my-app_1  |     at emitOne (events.js:116:13)
my-app_1  |     at Pool.emit (events.js:211:7)
my-app_1  |     at Connection.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/pool.js:274:12)
my-app_1  |     at Object.onceWrapper (events.js:317:30)
my-app_1  |     at emitTwo (events.js:126:13)
my-app_1  |     at Connection.emit (events.js:214:7)
my-app_1  |     at Socket.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/connection.js:177:49)
my-app_1  |     at Object.onceWrapper (events.js:315:30)
my-app_1  |     at emitOne (events.js:116:13)
my-app_1  |     at Socket.emit (events.js:211:7)
my-app_1  |     at emitErrorNT (internal/streams/destroy.js:64:8)
my-app_1  |     at _combinedTickCallback (internal/process/next_tick.js:138:11)
my-app_1  |     at process._tickCallback (internal/process/next_tick.js:180:9)
my-app_1  |   name: 'MongoError',
my-app_1  |   message: 'failed to connect to server [mongodb:27017] on first connect [MongoError: getaddrinfo ENOTFOUND mongodb mongodb:27017]' }
my-app_1  |
my-app_1  | If you are the maintainer of "createManager", then you can change its implementation to solve the problem (Most of the time, the solution is just to throw an actual Error instance instead.  Alternatively, if the goal was to indicate a particular exception, you could throw any of the special, reserved "exit signals"-- e.g. the code name of any of your defined exits besides "error" or "success").  Otherwise, please file a bug report with the maintainer, or fork your own copy and fix that.
my-app_1  |  [?] See https://sailsjs.com/support for help.
my-app_1  |     at Object.registerDatastore (/usr/src/app/node_modules/sails-mongo/lib/index.js:263:18)
my-app_1  |     at /usr/src/app/node_modules/waterline/lib/waterline.js:714:27
my-app_1  |     at /usr/src/app/node_modules/async/dist/async.js:3047:20
my-app_1  |     at eachOfArrayLike (/usr/src/app/node_modules/async/dist/async.js:1002:13)
my-app_1  |     at eachOf (/usr/src/app/node_modules/async/dist/async.js:1052:9)
my-app_1  |     at Object.eachLimit (/usr/src/app/node_modules/async/dist/async.js:3111:7)
my-app_1  |     at Object.initialize (/usr/src/app/node_modules/waterline/lib/waterline.js:650:11)
my-app_1  |     at buildOntologyAndRunAutoMigrations (/usr/src/app/node_modules/sails-hook-orm/lib/build-ontology-and-run-auto-migrations.js:55:7)
my-app_1  |     at async.auto._buildOntology (/usr/src/app/node_modules/sails-hook-orm/lib/initialize.js:456:7)
my-app_1  |     at runTask (/usr/src/app/node_modules/async/dist/async.js:1660:17)
my-app_1  |     at /usr/src/app/node_modules/async/dist/async.js:1602:17
my-app_1  |     at processQueue (/usr/src/app/node_modules/async/dist/async.js:1612:17)
my-app_1  |     at taskComplete (/usr/src/app/node_modules/async/dist/async.js:1630:13)
my-app_1  |     at /usr/src/app/node_modules/async/dist/async.js:1653:21
my-app_1  |     at /usr/src/app/node_modules/async/dist/async.js:339:31
my-app_1  |     at /usr/src/app/node_modules/async/dist/async.js:847:20
my-app_1  | ```
my-app_1  |     at Object.error (/usr/src/app/node_modules/sails-mongo/lib/index.js:268:21)
my-app_1  |     at /usr/src/app/node_modules/machine/lib/private/help-build-machine.js:1514:39
my-app_1  |     at proceedToFinalAfterExecLC (/usr/src/app/node_modules/parley/lib/private/Deferred.js:1149:14)
my-app_1  |     at proceedToInterceptsAndChecks (/usr/src/app/node_modules/parley/lib/private/Deferred.js:909:12)
my-app_1  |     at proceedToAfterExecSpinlocks (/usr/src/app/node_modules/parley/lib/private/Deferred.js:841:10)
my-app_1  |     at /usr/src/app/node_modules/parley/lib/private/Deferred.js:303:7
my-app_1  |     at /usr/src/app/node_modules/machine/lib/private/help-build-machine.js:952:35
my-app_1  |     at Function.handlerCbs.error (/usr/src/app/node_modules/machine/lib/private/help-build-machine.js:742:26)
my-app_1  |     at connectCb (/usr/src/app/node_modules/sails-mongo/lib/private/machines/create-manager.js:130:22)
my-app_1  |     at connectCallback (/usr/src/app/node_modules/mongodb/lib/mongo_client.js:428:5)
my-app_1  |     at /usr/src/app/node_modules/mongodb/lib/mongo_client.js:335:11
my-app_1  |     at _combinedTickCallback (internal/process/next_tick.js:131:7)
my-app_1  |     at process._tickCallback (internal/process/next_tick.js:180:9)

这是我的 /config/datastores.js 文件的样子:

module.exports.datastores = {

  default: {
    adapter: 'sails-mongo',
    url: 'mongodb://mongoDb:27017/my-app',
    ssl: false
  },
};

我还根据the Sails.js documentation修改了/config/models.js:

attributes: {
  createdAt: { type: 'number', autoCreatedAt: true, },
  updatedAt: { type: 'number', autoUpdatedAt: true, },
  id: { type: 'string', columnName: '_id' },
},

这是我的配置:

  • 节点 8.9.4
  • NPM 5.6.0
  • 帆 1.0.2
  • sails-mongo 1.0.1

值得注意的是,我在 Docker 容器中运行应用程序(我在过去版本的 Sails 中使用过类似的容器,没有任何问题)。我将如何解决这个问题?我似乎无法在其他地方找到任何解决方案。谢谢。

更新:

这是我的 docker-compose.yml 文件的样子:

version: '3.4'

services:
  server:
    image: my-app:latest
    build: .
    environment:
      NODE_ENV: development
      RUNNING_LOCALLY: isRunningLocally
    ports:
      - 1340:1337 # HOST_PORT is 1339 to avoid conflicts with other Sails.js apps running on host
    volumes:
      - ../:/usr/src/app
    entrypoint: nodemon

  mongoDb:
    image: mongo:3.6
    ports:
      - 27018:27017 # HOST_PORT is 27018 to avoid conflicts with other MongoDB databases running on host
    volumes:
      - ../../web-service/volumes/mongodb:/data/db # Since we want to use the same database as the web service

【问题讨论】:

  • 能否请您展示一下您的 Docker 配置(Dockerfile 或 docker-compose.yml)?
  • @VladyslavTurak 用 docker-compose.yml 文件更新了我的问题

标签: javascript node.js sails.js sails-mongo


【解决方案1】:

尝试在 docker-compose.yml 文件中将 MongoDB 服务的名称从 mongoDb 更改为 mongodb。

由于此错误消息:MongoError: failed to connect to server [mongodb:27017] on first connect [MongoError: getaddrinfo ENOTFOUND mongodb mongodb:27017],在我看来 Sails.js 1.0.2 在./config/datastores.js - 'mongodb://mongoDb:27017/my-app' 中的连接 URL 上运行 toLowerCase()。

您的 server 容器可以访问 mongoDb 容器中的 MongoDB。通过运行 toLowerCase() Sails.js 1.0.2 尝试连接到位于 mongodb 容器中的 MongoDB(mongodb 容器不存在,这就是它不起作用的原因)。

另外,不要忘记将您的 URL 连接从 mongodb://mongoDb:27017/my-app 更改为 mongodb://mongodb:27017/my-app。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    • 1970-01-01
    • 2014-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-12
    相关资源
    最近更新 更多