Eager loads didn't get last created child


#1

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
  .status(200)
  .json({
    message: 'Gambar sukses ditambah',
    newGambar, // I got the new child model here
    kost,
    movedGambar
  })

This is my middleware

await next()

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

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

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

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

  } else {
    return response
      .status(404)
      .json({
        message: 'Kost not found',
        kostId
      })
  }
....

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?


#2

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