Adonis 5 - Command Database IoC error

Hi all!

I’m trying to use Adonis 5, my controller and database looks fine in controllers. But what I’m trying to do now is to load some data, thus created a Command. The problem happens when I try to inject the database (as I do in my controller), but it does not work with the following error:

IoC Container
    at Function.lookupFailed (/Users/leo.ribeiro/Workspace/cpt/api-ans-igr/node_modules/@adonisjs/fold/build/src/Exceptions/IocLookupException.js:14:16)
    at Ioc.use (/Users/leo.ribeiro/Workspace/cpt/api-ans-igr/node_modules/@adonisjs/fold/build/src/Ioc/index.js:347:59)
    at Object.<anonymous> (/Users/leo.ribeiro/Workspace/cpt/api-ans-igr/commands/CargaAnual.ts:2:1)
    at Module._compile (internal/modules/cjs/loader.js:777:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:788:10)
    at Module.load (internal/modules/cjs/loader.js:643:32)
    at Function.Module._load (internal/modules/cjs/loader.js:556:12)
    at Module.require (internal/modules/cjs/loader.js:683:19)
    at require (internal/modules/cjs/helpers.js:16:16)
    at Object.esmRequire (/Users/leo.ribeiro/Workspace/cpt/api-ans-igr/node_modules/@poppinss/utils/build/src/esmRequire.js:20:38)

and this is my command code:

import { BaseCommand } from '@adonisjs/ace'
import Database from '@ioc:Adonis/Lucid/Database'

export default class CargaAnual extends BaseCommand {
  public static commandName = 'carga:anual'
  public static description = 'Executa Carga dos Arquivos da ANS IGR Anual'

  public async handle() {

    const instituicao = await Database
      .from('instituicoes')
      .select('*')
      .first()

    this.logger.info('instituicao', instituicao)
  }
}

Sorry if it’s a stupid issue, just newbie in Adonis. (My first day playin with the tool, and it’s so lovely! Coming from big projects and stitches with expressjs hehe)

1 Like

Hey @McSneaky, please could you help with this? I’ve not tried V5 yet.

1 Like

And just as a FYI, I can’t import the databaseConfig in my Command class file too…

When I try to import it, the same error happens, with the Env IoC, looks like: IocLookupException: E_IOC_LOOKUP_FAILED: Cannot resolve Adonis/Core/Env namespace from the IoC Container

I’m probably missing some import statement in my command class or the knowledge of how to inject in command files.

1 Like

Haven’t used commands in v5 yet but it seems like could start digging into src code from here:


To figure out how it works for Migrations, since they use DB too

Would also be worth a check what parameters come in into handle() and class constructor or might need to do import inside handle() function itself

Just throwing out some ideas :slightly_smiling_face:

3 Likes

This is the main reason I don’t want to try V5 until docs are out. The headaches might not be worth it.

1 Like

Nice reference! Thanks @McSneaky! Unfortunately didnt work too…

import { BaseCommand, args, Kernel } from "@adonisjs/ace";
import { inject } from "@adonisjs/fold";
import { DatabaseContract } from "@ioc:Adonis/Lucid/Database";

@inject([null, null, "Adonis/Lucid/Database"])
export default class CargaAnual extends BaseCommand {
  public static commandName = "carga:anual";
  public static description = "Executa Carga dos Arquivos da ANS IGR Anual";

  @args.string({ required: true, description: "Arquivo CSV da Carga" })
  public arquivo: string;

  constructor(
    app: ApplicationContract,
    kernel: Kernel,
    private db: DatabaseContract
  ) {
    super(app, kernel);
    console.info("creating carga anual!");
  }

:heavy_multiplication_x: fatal IocLookupException: E_IOC_LOOKUP_FAILED: Cannot resolve Adonis/Lucid/Database namespace from the IoC Container
at Function.lookupFailed (/Users/leo.ribeiro/Workspace/adcstuff/api-ans-igr/node_modules/@adonisjs/fold/build/src/Exceptions/IocLookupException.js:14:16)

1 Like

Error happens because of this line:

Remove it and add 4 params to constructor

constructor(hurr, durr, foo, bar) {
  // Log out those params to see what's in here
  console.log(hurr, durr, foo, bar)
}

Keep adding params and log them out until you get undefined as last param and see maybe there’s something that contains DB or something related to DB (or use arguments)

If you do this both for constructor and handle method I’m sure it starts to fold from somewhere.

1 Like

Didn’t work too… Actually the error was not in the import statement, it was being thrown in the @inject([null, null, "Adonis/Lucid/Database"]) statement… Any ideas?

I also checked the arguments of my constructor, it has only the following args:

  • ‘0’: Application
  • ‘1’: Kernel
1 Like

Here’s working command:

import { BaseCommand } from '@adonisjs/ace'
import { Kernel } from '@adonisjs/ace'
import { inject } from '@adonisjs/fold'
import { DatabaseContract } from '@ioc:Adonis/Lucid/Database'
import { ApplicationContract } from '@ioc:Adonis/Core/Application'

@inject([null, null, 'Adonis/Lucid/Database'])
export default class Hurr extends BaseCommand {
  public static commandName = 'hurr'
  public static description = 'Durr'

  constructor (app: ApplicationContract, kernel: Kernel, private db: DatabaseContract) {
    super(app, kernel)
  }

  public static settings = {
    loadApp: true,
  }

  public async handle () {
    console.log(this.db)

    this.logger.info('Hello world!')
  }
}

Key is to set loadApp: true in settings

4 Likes

Many thanks!

Had this same issues while trying to create a adhoc db:seed command. I had a look at the MigrationRun command like you said but somehow I missed the loadApp: true setting. Thanks a bunch man :smiley:

2 Likes