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 aconsole.log(userLogged)
check what returned– novic
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
– veroneseComS
Put the recording part?
– novic
let job = request.all()
? here comes the value?user_id
?– novic
"I do not receive from frontend the user_id, getting on account of backend save this data through token."
– veroneseComS
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 areturn (request.all());
to see what he returns! understood– novic
we are not in the same line of reasoning
– veroneseComS
request.all() does not have user_id
– veroneseComS
So you tested?
request.all() não possui o user_id
?– novic
no, my idea is to put it through the backend before saving
– veroneseComS
Let’s go continue this discussion in chat.
– novic