Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在AdonisJS中使用.save()函数时,超出了最大调用堆栈大小错误_Javascript_Node.js_Adonis.js - Fatal编程技术网

Javascript 在AdonisJS中使用.save()函数时,超出了最大调用堆栈大小错误

Javascript 在AdonisJS中使用.save()函数时,超出了最大调用堆栈大小错误,javascript,node.js,adonis.js,Javascript,Node.js,Adonis.js,我试图在AdonisJS中创建一个种子文件。我有一个简单的种子文件,它在数据库中添加了2个用户 UserSeeder.js 'use strict' /* |-------------------------------------------------------------------------- | UserSeeder |-------------------------------------------------------------------------- | | Ma

我试图在AdonisJS中创建一个种子文件。我有一个简单的种子文件,它在数据库中添加了2个用户

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

'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

“严格使用”
/*
|--------------------------------------------------------------------------
|用户播种机
|--------------------------------------------------------------------------
|
|使用Factory实例为数据库添加虚拟数据或
|直接使用清晰的模型。
|
*/
/**@type{import('@adonisjs/lucid/src/Factory')}*/
//const Factory=使用('Factory')
const User=use('App/Models/User')
类用户播种器{
异步运行(){
让用户=[
{
名字:'管理员',
lastname:'用户',
电邮:'admin@user.com',
密码:“abc123”,
角色编号:1,
是否处于活动状态:1
}, {
名字:“用户”,
姓:“约翰”,
电邮:'user@user.com',
密码:“abc123”,
角色编号:2,
是否处于活动状态:1
}
];
对于(var i=0;i
当我运行这个文件时,它显示“RangeError:超出了最大调用堆栈大小”

令人沮丧的是,我有另一个种子文件,运行完美。这是工作罚款:

roleseder.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

'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

“严格使用”
/*
|--------------------------------------------------------------------------
|RoleSeeder
|--------------------------------------------------------------------------
|
|使用Factory实例为数据库添加虚拟数据或
|直接使用清晰的模型。
|
*/
/**@type{import('@adonisjs/lucid/src/Factory')}*/
const Factory=使用('Factory')
const Role=use('App/Models/Role');
阶级角色扮演者{
异步运行(){
让角色=['Admin','Manager'];
对于(变量i=0;i
任何帮助都将不胜感激!
提前谢谢

我解决了这个问题。我在用户模型中使用getter方法时没有使用
get
关键字(facepalm),这使得它可以自调用,因此发生了递归。

如何调用导出的UserSeeder类/运行方法?这个错误通常是由于一个函数反复(有时是间接地)调用自身(也称为无限递归)。@JaredSmith我正在使用adonis命令运行这个文件
adonis seed--files=database/seeds/UserSeeder.js
。我不认为存在递归,它是一个运行2次的简单for循环。你看不到递归并不意味着它不存在,它可能存在于adonis中,这是控制框架反转的主要缺点之一。@JaredSmith同意!如果你在一两天内没有得到很大的动力,你可能想试试github。