I have cloned a https://github.com/beeman/loopback-angular-admin and I have created a couple of new roles using the loopback explorer and I am trying to get all the roles of a particular user when he logs in by loaded operation hook defined here -
https://docs.strongloop.com/display/public/LB/Operation+hooks#Operationhooks-loaded
like this -
user.observe('loaded', function appendRole(ctx, next){
if(ctx.instance){
user.findOne({
where: {
id: ctx.instance.id
},
include: {
"relation":"roles"
}
})
}
next();
})
so how do I return roles of a particular user using operation hooks. I am able to get all the roles of a user using loopback explorer using this api call
http://localhost:80/api/users/567ce48d6503f9404b56bb3e/roles?access_token=gyPzW3rpr3uzve2bUHtZQWv8iV5PfZYW7QLicCs4GwIKTdNA33SeRAlgPIQef7AE
UPDATE :
So, I tried adding the following code to the user.js -
user.observe('loaded', function appendRole(ctx, next){
if(ctx.instance){
console.log(ctx.instance.roles);
}
next();
})
and I am getting the following output in the console -
{ [Function]
_receiver:
{ username: '[email protected]',
password: '$2a$10$Bubhaq1LXFyCUn.W1/pEOewLSqspcP2GQlONwGH98V4HqCOAc9522',
email: '[email protected]',
status: 'created',
created: Mon Jan 04 2016 22:53:53 GMT+0530 (IST),
firstName: 'Harshit',
lastName: 'Laddha',
gender: 'male',
birthday: '1993-07-30T18:30:00.000Z',
qualification: 'sa;',
experience: 'askjdl',
achievements: 'sakldj',
street: 'has',
locality: 'alskjd',
area: 'lkjd',
city: 'bangalore',
id: 568aaaa997ace4670b5d9ac2 },
_scope:
{ where: { principalId: 568aaaa997ace4670b5d9ac2 },
collect: 'role',
include: 'role' },
_targetClass: 'Role',
getAsync: [Function],
build: [Function: build],
create: [Function],
updateAll: [Function: updateAll],
destroyAll: [Function: destroyAll],
findById: [Function],
findOne: [Function: findOne],
count: [Function: count],
destroy: [Function],
updateById: [Function],
exists: [Function],
add: [Function],
remove: [Function] }
so how do I get the roles of user using loaded operation hook