Come posso accedere ai dettagli dell'utente che sollevano la richiesta da un gancio modellorichiesta di accesso intestazioni da BeforeSave Modello Hook
Comment.beforeSave = function(next,com) {
//Want to add 2 more properties before saving
com.added_at = new Date();
com.added_by = //How can i set the user id here ??
//In case of a Remote hook i have ctx in param and i can get user id like this ctx.req.accessToken.userId; But in Model Hook how can i do the same?
next();
};
Esiste un modo per fare questo? Ho provato con il gancio remoto per l'articolo principale nel modo
MainItem.beforeRemote('**', function(ctx, user, next) {
if(ctx.methodString == 'leave_request.prototype.__create__comments'){
ctx.req.body.added_by = ctx.req.accessToken.userId;
ctx.req.body.added_at = new Date();
console.log("Added headers as .."+ctx.req.body.added_by);
}
else{
ctx.req.body.requested_at = new Date();
ctx.req.body.requested_by = ctx.req.accessToken.userId;
console.log("Added header @ else as .."+ctx.req.body.requested_by);
}
next();
});
e ho la log della console correttamente una volta faccio richiesta da esploratore, ma Ma l'esploratore mi restituiscono sempre l'errore
"errore": { "name": "ValidationError", "status": 422, "messaggio": "L'istanza comment
non è valido dettagli: added_by
non può essere vuoto; added_at
non può essere vuoto..", "statusCode": 422, "dettagli": { "contesto" : "commento", "codici": { "added_by": [ "presenza" ], "added_at": [ "presenza" ] }, "messaggi": { "added_by": [ "non può essere vuoto" ], "added_at": [ "può 't be blank' ] } }, "stack": "ValidationError: l'istanza comment
non è valida. Dettagli: added_by
non può essere vuoto; added_at
non può essere vuoto. \ N " }}
e il mio modello è come
"properties": {
"body": {
"type": "string",
"required": true
},
"added_by": {
"type": "number",
"required": true
},
"added_at": {
"type": "date",
"required": true
},
"leave_request_id":{
"type": "number",
"required": true
}
}
Il mio modello di commento non è esposto direttamente all'API. Quindi beforeRemote non eseguirà per questo. Il mio endpoint è MainItem//Comment. Quindi primaRemota verrà eseguito solo per MainItem –
In tal caso, sarà necessario impostare il gancio sul modello MainItem e provare a memorizzare l'ID utente in un globale o nelle impostazioni di commento. Ciò non è ottimale e potrebbe causare errori nelle richieste simultanee. Mi dispiace per quello. – amenadiel
Ora ho una domanda edita, puoi aiutarmi ora? –