Search code examples
javascriptnode.jssails.jssails-mongo

Sails.js "Could not tear down ORM hook" error with sails-mongo


I recently updated Sails from 0.12 to 1.0.2 on my laptop and started a new project. I'm trying to connect to a MongoDB database using sails-mongo but I get the following error:

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)

This is what my /config/datastores.js file looks like:

module.exports.datastores = {

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

I've also modified /config/models.js as per the Sails.js documentation:

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

This is what my configuration looks like:

  • Node 8.9.4
  • NPM 5.6.0
  • Sails 1.0.2
  • sails-mongo 1.0.1

Worth noting that I am running the application inside a Docker container (I've used a similar container with past versions of Sails without any issues). How would I resolve this? I can't seem to find any solutions to this elsewhere. Thanks.

UPDATE:

This is what my docker-compose.yml file looks like:

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

Solution

  • Try to change the MongoDB service's name from mongoDb to mongodb in the docker-compose.yml file.

    Due to this error message: MongoError: failed to connect to server [mongodb:27017] on first connect [MongoError: getaddrinfo ENOTFOUND mongodb mongodb:27017], it seems to me that Sails.js 1.0.2 runs toLowerCase() on the connection URL you have in the ./config/datastores.js - 'mongodb://mongoDb:27017/my-app'.

    Your server container has access to the MongoDB that is in the mongoDb container. By running toLowerCase() Sails.js 1.0.2 tries to connect to the MongoDB that is in the mongodb container (the mongodb container does not exist and that is why it does not work).

    Also, do not forget to change your URL connection from mongodb://mongoDb:27017/my-app to mongodb://mongodb:27017/my-app.