You can do this in two ways.
First you need to add this data to the controller. Sort of
// /api/controllers/mycontroller.js module.exports = { new: function(req, res) { if (typeof req.user.id !== 'undefined') { req.body.createdBy = req.user.id; // req.body or req.params } MyModel.create(req.body /* ... */) } }
If you have a lot of data manipulation using MyModel , this can be annoying. This way you can add a static method to your model to save it with a user id. Sort of:
// /api/models/myModel.js module.exports = { attributes: {/* ... */}, createFromRequest: function(req, cb) { // do anything you want with your request // for example add user id to req.body if (typeof req.user.id !== 'undefined') { req.body.createdBy = req.user.id; } MyModel.create(req.body, cb); } }
And using it in your controller
// /api/controllers/mycontroller.js module.exports = { new: function(req, res) { MyModel.createFromRequest(req, function(err, data) { res.send(data); }); } }
source share