module.exports = function(Self) { let loopBackContext = require('loopback-context'); Self.validate('text', isEnabled, {message: 'Se debe rellenar el campo de texto'}); function isEnabled(err) { if (!this.text) err(); } Self.observe('before save', function(ctx, next) { ctx.instance.created = Date(); let currentUser = loopBackContext.getCurrentContext(); let userId = currentUser.get('currentUser'); let app = require('../../server/server'); let Employee = app.models.Employee; Employee.findOne({where: {userFk: userId}}, function (err, user){ if (user){ ctx.instance.employeeFk = user.id; next(); } }); }); };