Middleware that saves a user_id after a request to Adonis.js/Node.js

Asked

Viewed 377 times

0

I own the following table job:

job
--------------
id INTEGER PK,
name VARCHAR,
user_id INTEGER FK

I need to save in the column user_id the id of the user who created that record. I tried to create this middleware:

'use strict'

class Auditory{
  async handle ({ request, auth }, next) {

    const userLogged= await auth.getUser()
    const idUser= userLogged.id

    request.user_id = idUser


    await next()
  }
}

module.Exports = Audit I put a console.log() within the Auditory class, so I know that this midlware is being called when I make the request, but the user_id is not being saved in the database.

If I put one console.log() in the idUser I get the correct value of the expected id, but when looking at the bank what was saved, the user_id is null.

I’m forgetting something?

I do not receive from the frontend the user_id, being on account of the backend save this data through the token.

@Edit:

Recording as requested:

async store ({ request }){

    let job= request.all()
    job = await Job.create(job)
    return job

}
  • Is coming null ? put a console.log(userLogged) check what returned

  • 1

    no, the return of the variable userLogged and idUsuario is as expected. The problem is that I saw it in the request and saved it as null, I tried settar in the request.body and request before and after await next() but both cases save user_id as null

  • Put the recording part?

  • let job = request.all()? here comes the value? user_id?

  • "I do not receive from frontend the user_id, getting on account of backend save this data through token."

  • That I know "Eu não recebo do frontend o user_id, ficando por conta do backend salvar este dado através do token."? you can redeem in front - end only what you need by giving a return (request.all()); to see what he returns! understood

  • we are not in the same line of reasoning

  • request.all() does not have user_id

  • So you tested? request.all() não possui o user_id?

  • no, my idea is to put it through the backend before saving

Show 6 more comments

1 answer

1


With the help of @Virgilio Novic I managed to reach the expected result through the following implementation:

I created this middleware:

class Auditoria {
    async handle ({ request, auth, }, next) {

        const usuarioLogado = await auth.getUser()

        request.user_id = usuarioLogado.id
        await next()   
    }
}

In my store I create a user object with the request.all() data and add the user_id that came from middleware:

async store ({ request }){

    const user = {...request.all(), user_id: request.user_id};
    const response = await User.create(user)

    return response

}

My main problem was thinking that request.all() would contain the middleware user_id, but the method request.all() is a method that does not take the dynamic created by the middleware request.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.