Seeding - asynchronous problem


Hey guys,
I try to save a .csv file into my database. For reading csv row by row i use fast-csv. For every row, i want to create a database entry.

This is my code inside the SeedFile.

class StationSeeder {
  async run () {
    try {
      csv.fromPath(file, {delimiter: ';', headers: true})
      .on("data", function(data){
          let station = new Station()
 = data[0]

      .on("end", function(){
    } catch (error) {

Reading csv file works fine - but it creates no database entries. I guess it is because of the asynchronous behaviour, station got overritten before could create the entry.
I know i should use await, but that gives me an error:


SyntaxError: Unexpected identifier

Makes sense, because I can use await only with async, but the fast-csv method is not async I guess.

I have no idea how to solve that problem so I would appreciate your help! :slight_smile:


Your callback must be async.

May you try

csv.fromPath(file, { delimiter: ';', headers: true })
  .on('data', async function (data){
    let station = new Station() = data[0]


Run the seed command with --keep-alive command.

adonis seed --keep-alive


Thank you both for your solutions! :slight_smile:
First one does not worked for me in this case, but I solved it by using an explicitly synchronous reader.

Next time I will try yout solution virk, thank you.