How can I access the details of a user who raises a query using Model Hook
Comment.beforeSave = function(next,com) {
Is there any way to do this? I tried using Remote hook for the main element in the path.
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();
});
And I get console logs correctly as soon as I make a request from Explorer, but Explorer always returns me an error
"error": {"name": "ValidationError", "status": 422, "message": "The comment instance is not valid. Details: added_by cannot be empty, added_at cannot be empty.", "statusCode": 422, "Details": {"context": "comment", "codes": {"Added": ["presence"], "added_at": ["presence"]}, "Messages": {"Added": [" cannot be empty "]," added_at ": [" cannot be empty "]}}," stack ":" ValidationError: comment instance is invalid. Details: added_by cannot be empty, added_at cannot be empty. \ n " }}
and my model is like
"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 }
}
loopbackjs strongloop
Jibin mathew
source share