I can't instance of class extends Model

First i’m created Model:

adonis make:model User

after calling instance:

let user = new User();

Error:

warning: 
  WARNING: Adonis has detected an unhandled promise rejection, which may
  cause undesired behavior in production.
  To stop this warning, use catch() on promises or wrap await
  calls inside try/catch.

TypeError: User is not a constructor
 

In all model documentation, there is no constructor declaration

1 Like

Can you share the code where you instantiated the User class?

1 Like

This is a class that instantiates the Class Model.

Where

let ssh = new SSh ();

Read Ssh instead of User

'use strict'

const {Command} = use('discord-akairo');
const {SSh} = use('App/Models/SSh');

class SshCreateCommand extends Command {
    constructor() {
        super('ssh', {
            aliases: ['ssh'],
            args: [
                {
                    id: 'action',
                    type: 'lowercase',
                    flag: 'action',
                    prompt: {
                        start: message => `Hey ${message.author}, se precisar é só digitar \`!ssh (new|delete|show)\``,
                    }
                },
                {
                    id: 'alias',
                    type: 'lowercase',
                    flag: 'alias',
                },
                {
                    id: 'port',
                    type: 'integer',
                },
                {
                    id: 'user',
                    type: 'lowercase',
                },
                {
                    id: 'host',
                    type: 'number',
                },
                {
                    id: 'pass',
                    type: 'number',
                }
            ],
        });
    }

    async exec(message, args) {

        if (args.action === 'create' || args.action === 'new') {
            let ssh = new SSh();

            // ssh.alias = "alias";
            // ssh.port = "123";
            // ssh.user = "user";
            // ssh.host = "host";
            // await ssh.create();
        } else if (args.action === 'delete' || args.action === 'del') {
            console.log('É pra excluir alguma coisa');
        } else if (args.action === 'show' || args.action === 'status') {
            console.log('mostrar detalhado');
        } else {
            return message.reply(`Hey ${message.author}, se precisar é só digitar \`!ssh (new|delete|show)\``);
        }

        return message.reply(`The sum is ${args.alias}!`);
    }
}

module.exports = SshCreateCommand;

I see.

By default, AdonisJS’ controller classes do not include constructors. but that does not stop you from adding the constructors yourself.

So in the SshController (for example), add a constructor:

const Connection = use('connection') // An example

class SshController {
  constructor(alias, port, user, host) {
    this.alias = alias
    this.port = port
    this.user = user
    this.host = host
  }
  async create () {
    // Your create logic here
    // e.g.
    await Connection.create({
      alias: this.alias,
      port: this.port,
      user: this.user,
      host: this.host
    })
    
  }
}

module.exports = SshController

Then import SshController into your SshCreateCommand class

const Ssh = use('App/Controllers/Http/SshController')

class SshCreateCommand extends Command {
  constructor() {
    super('ssh', {
      aliases: ['ssh'],
      args: [
        {
          id: 'action',
          type: 'lowercase',
          flag: 'action',
          prompt: {
            start: (message) => `Hey ${message.author}, se precisar é só digitar \`!ssh (new|delete|show)\``,
          },
        },
        {
          id: 'alias',
          type: 'lowercase',
          flag: 'alias',
        },
        {
          id: 'port',
          type: 'integer',
        },
        {
          id: 'user',
          type: 'lowercase',
        },
        {
          id: 'host',
          type: 'number',
        },
        {
          id: 'pass',
          type: 'number',
        },
      ],
    });
  }

  async exec(message, args) {
    if (args.action === 'create' || args.action === 'new') {
      // Instantiate the Ssh class with the properties to be received by the class' constructor.
      let ssh = new SSh('alias', '123', 'user', 'host');
      await ssh.create();

      // You cannot do all these directly on the class instance.
      // ssh.alias = "alias";
      // ssh.port = "123";
      // ssh.user = "user";
      // ssh.host = "host";

    } else if (args.action === 'delete' || args.action === 'del') {
      console.log('É pra excluir alguma coisa');
    } else if (args.action === 'show' || args.action === 'status') {
      console.log('mostrar detalhado');
    } else {
      return message.reply(`Hey ${message.author}, se precisar é só digitar \`!ssh (new|delete|show)\``);
    }

    return message.reply(`The sum is ${args.alias}!`);
  }
}

module.exports = SshCreateCommand;

This isn’t tested for errors.

When you refer:

It would be the call to the model. which is responsible for metdh .create (since you extend the framewors model class)

Would writing correctly look like this?

'use strict'

const {SSh} = use('App/Models/SSh');

class SshController {
    constructor(alias, port, user, host) {
        this.alias = alias
        this.port = port
        this.user = user
        this.host = host
    }

    async create () {
        await Ssh.create({
            alias: this.alias,
            port: this.port,
            user: this.user,
            host: this.host
        })
    }
}

module.exports = SshController

Oh! Sorry for the next mixup. I thought Ssh was a custom class.

If Ssh is a class of type 'Model', then you do not need to instantiate at all. In that case, create is a static method.

There is no need to destructure as you did here:

const {Ssh} = use('App/Models/Ssh')

Just do:

const Ssh = use('App/Models/Ssh')

Then:

const Ssh = use('App/Models/Ssh')

class SshCreateCommand extends Command {
  constructor() {
    super('ssh', {
      aliases: ['ssh'],
      args: [
        {
          id: 'action',
          type: 'lowercase',
          flag: 'action',
          prompt: {
            start: (message) => `Hey ${message.author}, se precisar é só digitar \`!ssh (new|delete|show)\``,
          },
        },
        {
          id: 'alias',
          type: 'lowercase',
          flag: 'alias',
        },
        {
          id: 'port',
          type: 'integer',
        },
        {
          id: 'user',
          type: 'lowercase',
        },
        {
          id: 'host',
          type: 'number',
        },
        {
          id: 'pass',
          type: 'number',
        },
      ],
    });
  }

  async exec(message, args) {
    if (args.action === 'create' || args.action === 'new') {
      await Ssh.create({
        alias: "alias",
        port: "123",
        user: "user",
        host: "host",
      });

    } else if (args.action === 'delete' || args.action === 'del') {
      console.log('É pra excluir alguma coisa');
    } else if (args.action === 'show' || args.action === 'status') {
      console.log('mostrar detalhado');
    } else {
      return message.reply(`Hey ${message.author}, se precisar é só digitar \`!ssh (new|delete|show)\``);
    }

    return message.reply(`The sum is ${args.alias}!`);
  }
}

module.exports = SshCreateCommand;
1 Like

Now it made sense: D
But I’m not trying to succeed.

In my ssh-create.js

'use strict'

const { Command } = require('discord-akairo');
const Ssh = use('App/Models/Ssh');

when run: adonis serve --dev

Return in console:

Error: Cannot find module ‘/home/milrak/Documentos/status-server-bot/app/Models/Ssh’

highlighted in red:

const Ssh = use(‘App/Models/Ssh’);

My class Ssh:

'use strict'

/** @type {typeof import('@adonisjs/lucid/src/Lucid/Model')} */
const Model = use('Model');

class SSh extends Model {

}

module.exports = SSh

Fix the spelling error here and it will fix this error: Error: Cannot find module ‘/home/milrak/Documentos/status-server-bot/app/Models/Ssh’

Ssh not SSh.

Cheers!

1 Like

I love you! :joy:
Thank you very much for your patience! :handshake:

1 Like

You are welcome, mate. :heart: