4

I am trying to create a seeder file in AdonisJS. I have a simple seeder file which adds 2 users in the database.

UserSeeder.js

'use strict'

/*
|--------------------------------------------------------------------------
| UserSeeder
|--------------------------------------------------------------------------
|
| Make use of the Factory instance to seed database with dummy data or
| make use of Lucid models directly.
|
*/

/** @type {import('@adonisjs/lucid/src/Factory')} */
// const Factory = use('Factory')

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

class UserSeeder {
  async run () {

    let users = [
      {
        firstname: 'Admin',
        lastname: 'User',
        email: 'admin@user.com',
        password: 'abc123',
        role_id: 1,
        is_active: 1
      }, {
        firstname: 'User',
        lastname: 'John',
        email: 'user@user.com',
        password: 'abc123',
        role_id: 2,
        is_active: 1
      }
    ];
    
    for (var i = 0; i < users.length; i++) {
      const user = new User();
      user.firstname = users[i].firstname;
      user.lastname = users[i].lastname;
      user.email = users[i].email;
      user.password = users[i].password;
      user.role_id = users[i].role_id;
      user.is_active = users[i].is_active;
      
      await user.save();
    }

  }
}

module.exports = UserSeeder

when I run this file, it says "RangeError: Maximum call stack size exceeded".

The frustrating part is that I have another seeder file that runs perfectly. Here is the working fine:

RoleSeeder.js

'use strict'

/*
|--------------------------------------------------------------------------
| RoleSeeder
|--------------------------------------------------------------------------
|
| Make use of the Factory instance to seed database with dummy data or
| make use of Lucid models directly.
|
*/

/** @type {import('@adonisjs/lucid/src/Factory')} */
const Factory = use('Factory')

const Role = use('App/Models/Role');

class RoleSeeder {
  async run () {
    let roles = ['Admin', 'Manager'];
    for (var i = 0; i < roles.length; i++) {
      const role = new Role()
      role.name = roles[i]

      await role.save()
    }
  }
}

module.exports = RoleSeeder

Any help would be appreciated! Thanks in advance

1 Answers1

2

I figured out the problem. I was using getter methods in the User model without a get keyword (facepalm) which made it self calling and hence recursion occurred.