Cannot read property 'name' of undefined at Factory.model


#1

i have create seed. then i have run this seed and i try to createMany record in my databse. but every time get this error.

Cannot read property ‘name’ of undefined
at Factory.model

my code is like this:

let array = [{
name : ‘site name’
}];

await Factory.model(‘App/Models/Settings’).createMany(array.length, array);

// await Factory.model(‘App/Models/Settings’).createMany(array);

await Factory.blueprint(‘App/Models/Currency’, (fake, index, data) => {
return {
name: data[index].name
}
})


#2

Hey @bhavesh1992! :wave:

Your code is wrong. What are you trying to achieve? (explain it with plain words)


#3

i have try to create some global settings create in my database settings table on seed run.


#4

Do these global settings records have anything other than a name field you need to populate?

I wouldn’t use factory in a seeder, personally… Factories are, to my mind, really for testing not seeding real data.

I have a settings table in the db for one of my projects, it’s schema consists of ‘id’ and ‘value’ fields… the ‘id’ being the string identifier of the setting name, rather than an auto incrementing id, that way I can just say like Settings.find('some_setting') or whatever…

Anyway, this is basically how I do my seeding for that table:

'use strict'

const Database = use('Database')

class SettingsSeeder {
  async run () {
    // Clear table before seeding...
    await Database.table('settings').truncate()

    // Populate settings records!
    await Database.table('settings').insert([
      { id: 'site_name', value: 'My Awesome Website' },
      { id: 'timezone', value: 'UTC' },
      { id: 'something_else', value: 'blah' },
      // ... etc ...
    ])
  }
}

module.exports = SettingsSeeder