-1

I created a function in a class that inserts a data in the users table. I have a data property that stores the all inserted data. My current problem is, whenever I will try to log the data, it returns an empty array.

I can confirm that there is nothing wrong with prisma, because the it logs the actual inserted data (there is a console log in the code below). I suspect that this could be promise issue, but I can't confirm and until now I'm unable to find solution.

I have tried wrapping the forEach by Promise.all(..), but it returns a typescript error. I would appreciate any comment or suggestion, since I already burned over an hour of this problem.

my class:

class StudentFac {
  public data: IProvider[] = []

  public run = async () => {
    ....
  
    data.forEach(async (data, index) => {
      this.data[index] = await prisma.user.create({ data })
      console.log(this.data[index])  <------------ it displays actual data here
    })
  }
}

how I use:

const studentFac = new StudentFac()
await studentFac.run();

console.log(studentFac.data) <--------------- returns empty array
Joulukuusi
  • 3,164
  • 6
  • 33
  • 53
Johnny
  • 447
  • 2
  • 13

0 Answers0