Stuck data after redirect

#1

I wanna ask, after I do an insert then I redirect my controller with Session.flash() and following by response.route(), but after it redirects to my route the data wasn’t updated, it doesn’t show the new row but the session flash message is shown. I need to reload my page to see the new data.

I also try with response.redirect() and it still has the same result like response.route()

Here is my code

async index({request, response, view}){
        const todos = await Todo.all()

        return view.render('index', { todos: todos.rows })
    }

store({request, response, view, session}){
        const todo = new Todo()

        todo.title = request.input('title')
        todo.description = request.input('description')
        todo.save()

        session.flash({ notification: 'Successfully create!' })
        response.route('Todo.index')
    }

Is anyone here could help me? Thank you so much.

0 Likes

#2

excuse me but if I understand you try to update a data, so that code doesn’t do that, because you’re creating a new instance so you’re creating a new record, you will need yo do const todo = Todo.findOrFail(params.id)

0 Likes

#3

Thanks for reminder, actually that’s only my example code. The method of update is running well, just the redirect method is the only my trouble.

0 Likes

#4

Hi, I think there was a problem in the store function.
The model.save() is a async function which it will return a Promise, so you will need to add await to wait for resolve and also to the store function declaration.

please try to modify your code

async store({request, response, view, session}){
        const todo = new Todo()

        todo.title = request.input('title')
        todo.description = request.input('description')
        await todo.save()

        session.flash({ notification: 'Successfully create!' })
        response.route('Todo.index')
    }
3 Likes

#5

Damn, it works like a charm! I didn’t think that the save method doesn’t need the await method. Thanks dude!

0 Likes

#6

All operations involving the database are asynchronous by nature. @kiddyxyz

0 Likes

#7

all right, thanks mate!

0 Likes