Eager loads didn't get last created child


Hi, I’m getting issue about eager load

I have one to many relationship schema
In controller, I created a new child model
Then, I called an after middleware to get the parent model with all of its children models
But in the middleware, it only get the last children models without the new child model I created in the controller

This is my controller

const movedGambar = gambar.toJSON()
// const newGambar = await Gambar.create({
//   kost_id: kost.id,
//   gambar: movedGambar.fileName
// })
const newGambar = await kost.gambars().create({ gambar: movedGambar.fileName })

return response
    message: 'Gambar sukses ditambah',
    newGambar, // I got the new child model here

This is my middleware

await next()

if (response.responseCode !== 200) {
  return response

const kostId = response.lazyBody.content.kost.id

try {
  const kost = await Kost
    .with('gambars') // here I got the children without the new created one
    .where({ id: kostId })

  if (kost) {
    // request.body.kost = kost
    response.lazyBody.content.kost = kost
    return response

  } else {
    return response
        message: 'Kost not found',

For example I have 5 children before I call the controller
I want it returns 6 children after I call the controller, but with those eager loads, it only returned 5 children

Here I have the result

Anyone has solutions?


Sorry, this is my fault

// this should be response.response.statusCode
if (response.responseCode !== 200) {
  return response

I got the old children because I return the old response before it loads the eager