How can i access the details of the user who raise the request from a Model Hook
Comment.beforeSave = function(next,com) {
//Want to add 2 more properties
I solved this by adding the middleware for body parsing. In the middleware.js I wrote the following code:
...
"parse": {
"body-parser#json": {},
"body-parser#urlencoded": {"params": { "extended": true }}
},
...
Also, in the server.js I added the require for the body parser and multer:
var loopback = require('loopback');
...
var bodyParser = require('body-parser');
var multer = require('multer');
...
app.use(bodyParser.json()); // application/json
app.use(bodyParser.urlencoded({ extended: true })); // application/x-www-form-urlencoded
app.use(multer()); // multipart/form-data
...
Then add the dependencies in the package.json
"body-parser": "^1.12.4",
"multer": "^0.1.8"
Now you can do things like the following in the /models/user.js (for any model)
user.beforeRemote('create', function(ctx, unused, next) {
console.log("The registered user is: " + ctx.req.body.email);
next();
});
I hope this helps! :)