2

How to foreach data and update data in loop using adonisjs I wannt to do something like this

in php I do this

$emp = Employee::all()

foreach($emp as $data) {
 $emp_store = Store::where('emp_id', $emp->id);
 $emp_store->name = $emp_name;
 $emp->save()
}

but after I change into adonisjs How can I do something like this in Controller . Now I try to do

const emp =  await Employee.all();
    for(var val of emp) {
     // I want to update data using each emp id
      console.log(val)
    }

after I try to do I got an error said emp is not iterable

TryHard
  • 299
  • 4
  • 19

2 Answers2

3

I just find it

for(let i in emp.rows) {
      const lobby = emp.rows[i]
      console.log(lobby) // you should be able to have access to name now
}

thnks

TryHard
  • 299
  • 4
  • 19
0

To convert a serializable instance to a plain array/object, call its toJSON method:

const json = emp.toJSON()

With this you can loop through the array

Pepe
  • 380
  • 1
  • 4
  • 12