[英]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.