javascriptsails.js

Why aren't models being imported in my Sails application?


I'm helping someone out who's got existing code written in JavaScript using the Sails framework. For context, I currently have zero experience with this framework.

Why aren't the application's models being imported?

More specifically and concretely, when I try to run the code, it appears that the models the developer has defined aren't getting imported anywhere. For example, when I try to call the API to register a new user, I get this error:

ReferenceError: User is not defined
    at Object.fn (/app/api/controllers/auth/register.js:58:23)
    at wrapper (/node_modules/@sailshq/lodash/lib/index.js:3282:19)
    at parley.retry [as _handleExec] (/node_modules/machine/lib/private/help-build-machine.js:1016:29)
    at Deferred.exec (/node_modules/parley/lib/private/Deferred.js:286:10)
    at Deferred.switch (/node_modules/machine/lib/private/help-build-machine.js:1469:16)
    at Object._requestHandler [as auth/register] (/node_modules/machine-as-action/lib/machine-as-action.js:1153:27)
    at /node_modules/sails/lib/router/bind.js:248:46
    at routeTargetFnWrapper (/node_modules/sails/lib/router/bind.js:395:9)
    at Layer.handle [as handle_request] (/node_modules/express/lib/router/layer.js:95:5)
    at next (/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/node_modules/express/lib/router/layer.js:95:5)
    at /node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/node_modules/express/lib/router/index.js:341:12)
    at next (/node_modules/express/lib/router/index.js:275:10)
    at next (/node_modules/express/lib/router/route.js:127:14)

This suggests to me that the models (which would include the User object referenced here, and that does exist in a User.js file) aren't getting imported correctly. I had a similar error with the bootstrap.js file, which was trying to instantiate a Settings object and didn't know what that was. (I "solved" that problem to get the app to boot by commenting out all the logic in the bootstrap file.)

I'm running this with a pretty minimal Docker container via Docker Compose. I have checked, and the various model files are present when I SSH into the container, so it's not an obvious containerization issue.

Dockerfile

FROM node:slim

COPY package.json ./

RUN npm install

docker-compose.yml

version: "3.7"
services:
  db:
    image: postgres
    restart: always
    environment:
      POSTGRES_PASSWORD: localpassword
    ports:
      - "5432:5432"
    volumes:
      - pgdata:/var/lib/postgresql/data
    networks:
      default:
        aliases:
          - database

  backend:
    build:
      dockerfile: Dockerfile
    working_dir: /app
    command: ["node", "app.js"]
    ports:
     - "1337:1337"
    networks:
      default:
        aliases:
          - backend
    volumes:
      - ./:/app

networks:
  default:

volumes:
  pgdata:
    driver: local

Solution

  • As it turns out, the Sails framework (or something else in the stack) is giving a somewhat misleading traceback.

    The application wasn't correctly configured to connect to the database. If Sails can't connect to the database as part of the model import, it seems to fail silently on that, and instead fails to import the models.

    Correctly connecting the database to the application fixed this problem and got the models to load.