首页 > 解决方案 > Adonis factory TypeError:无法读取未定义的属性“名称”

问题描述

这可能已经被问过了,但我找到的答案对我不起作用,所以我希望有人帮助我:)

最近我一直在尝试为我的数据库播种,但我遇到了一个问题..我一直收到这个错误:TypeError: Cannot read property 'name' of undefined

深入挖掘,我发现了错误,但由于我是这个世界的新手,我无法自己修复它..

我的工厂.js

const Factory = use('Factory')
Factory.blueprint('App/Models/User', async faker => {`
   return {`
        name: faker.first(),
        surname: faker.last(),
        email: faker.email({ domain: 'somedomain.br' }),
        password: 'secret'
    }
})

我的 ClientSeeder.js

class ClientSeeder {
  async run() {
    const role = await Role.findBy('slug', 'client')
    console.log(role);
    const clients = await Factory.model('App/Models/User').createMany(30)
    await Promise.all(clients.map(async client => {
      await clients.roles().attach([role.id])
    }))
[...]

我得到的错误

TypeError: Cannot read property 'name' of undefined
    at Factory.model (/mnt/c/wamp64/www/ecommerce/node_modules/@adonisjs/lucid/src/Factory/index.js:97:39)
    at ClientSeeder.run (/mnt/c/wamp64/www/ecommerce/database/seeds/0002_ClientSeeder.js:24:35)
    at async SeedDatabase.handle (/mnt/c/wamp64/www/ecommerce/node_modules/@adonisjs/lucid/commands/Seed.js:135:11)
    at async /mnt/c/wamp64/www/ecommerce/node_modules/@adonisjs/ace/src/Command/index.js:565:26

这就是我说我挖的部分:node_modules/@adonisjs/lucid/src/Factory/index.js:97:39就在 97:39

  model(name) {
    console.log(`Valor saindo: ${name}`);
    const blueprint = this.getBlueprint(name)
    console.log(`CHEGOU AQUI O BLUEPRINT ${blueprint}`);
    return new ModelFactory(blueprint.name, blueprint.callback)
  }
//console.log(name) returns 'App/Models/User'
//console.log(blueprint) returns undefined;

现在,在 getBlueprint()

  getBlueprint(name) {
    console.log(`Nome retornado: ${name}`);
    let bp = this._blueprints.find((blueprint) => blueprint.name === name)
    console.log(`O BP DESSA PORRA É ${bp}`);
    return
  }

//console.log(name) returns 'App/Models/User'
//console.log(bp) returns 'undefined'

这里有人知道发生了什么吗?

包版本

"@adonisjs/ace": "^5.0.8",
"@adonisjs/auth": "^3.0.7",
"@adonisjs/bodyparser": "^2.0.5",
"@adonisjs/cors": "^1.0.7",
"@adonisjs/fold": "^4.0.9",
"@adonisjs/framework": "^5.0.9",
"@adonisjs/ignitor": "^2.0.8",
"@adonisjs/lucid": "^6.1.3",
"@adonisjs/mail": "^3.0.10",
"@adonisjs/validator": "^5.0.6",
"@adonisjs/websocket": "^1.0.12",
"adonis-acl": "^1.1.1",
"adonis-bumblebee": "^2.2.0",
"mysql": "^2.18.1"

迁移/Users.js

class UserSchema extends Schema {
  up() {
    this.create('users', (table) => {
      table.increments()
      table.string('name', 80).notNullable()
      table.string('surname', 80).notNullable()
      table.string('email', 254).notNullable().unique()
      table.string('password', 60).notNullable()
      table.integer('image_id').unique().unsigned()
      table.timestamps()
    })
  }

  down() {
    this.drop('users')
  }
}

Node.js 和 npm 版本

节点:v14.1.0

npm:6.14.7

标签: adonis.jsseeding

解决方案


推荐阅读