简体   繁体   English

在 AdonisJS 中使用.save() function 时超出最大调用堆栈大小错误

[英]Maximum call stack size exceeded error while using .save() function in AdonisJS

I am trying to create a seeder file in AdonisJS.我正在尝试在 AdonisJS 中创建一个播种器文件。 I have a simple seeder file which adds 2 users in the database.我有一个简单的播种器文件,它在数据库中添加了 2 个用户。

UserSeeder.js 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".当我运行此文件时,它显示“RangeError:超出最大调用堆栈大小”。

The frustrating part is that I have another seeder file that runs perfectly.令人沮丧的部分是我有另一个运行良好的播种器文件。 Here is the working fine:这是工作正常:

RoleSeeder.js 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提前致谢

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.我在用户 model 中使用了 getter 方法,但没有使用get关键字(facepalm),这使它自我调用,因此发生了递归。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用上下文时超出最大调用堆栈大小和 ENOENT 错误 - Maximum Call Stack Size exceeded and ENOENT error while using context 阶乘函数运行错误:超出最大调用堆栈大小 - factorial function running error : Maximum call stack size exceeded 收敛系列函数上的最大调用堆栈大小超出错误 - Maximum call stack size exceeded error on converging series function 没有递归函数,仍然出现最大调用堆栈大小超出错误 - No recursive function, still getting a Maximum call stack size exceeded error firebase 函数未处理的错误 RangeError:超出最大调用堆栈大小 - firebase function Unhandled error RangeError: Maximum call stack size exceeded onCall Firebase函数出现“超出最大调用堆栈大小”错误 - 'Maximum call stack size exceeded' error with onCall firebase function 错误RangeError:使用setTimeout时超出最大调用堆栈大小 - ERROR RangeError: Maximum call stack size exceeded when using setTimeout 在滚动函数上超出最大调用堆栈大小 - Maximum call stack size exceeded , on an scroll function 递归函数中超出了最大调用堆栈大小 - Maximum call stack size exceeded in recursive function 递归函数:超出最大调用堆栈大小 - Recursive Function: Maximum call stack size exceeded
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM