javascript - JS Object strange behaviour when trying access Loopback related model query -
i working loopback framework, doing web project. think question exposing here has less this, general javascript / node.js knowledge.
at 1 part of code, doing:
rolemapping.find({ where: { principaltype: 'user', principalid: context.principals[0].id }, include: 'role' }, function(err, roles){ console.log(roles[0]); (var in roles) { if (roles[i].role.name === 'teamleader' && roles[i].groupid === context.modelid) { cb(null,true); }else { cb(null,false); } } });
ok this, fails when trying compare roles[i].role.name
. so, went logging roles[i]
object contained.
{ groupid: 1, id: 3, principaltype: 'user', principalid: 1, roleid: 2, role: { id: 2, name: 'teamleader', description: 'the leader(s) of team', created: null, modified: null } }
ok, nothing wrong, still fails, tried print role
property. , surprise:
{ [function] update: [function], destroy: [function], create: [function], build: [function], _targetclass: 'role' }
so, role
property seems sort of function? how been correctly printed before?
eventually, lost in frustration tried var role = json.parse(json.stringify(roles[i]));
and access every property of object normally, not clean nor normal.
this blew mind first time in years of js programming (sort of amateurish though), , pleased if clarify me. thanks
edit: seems specific framework, i'm changing title community.
i found issue 1425 links following docs:
with node.js api, need call
tojson()
convert returned model instance related items plain json objectplease note relation properties […] points javascript function relation method.
so seems have use
for (var i=0; i<roles.length; i++) { var x = roles[i].tojson(); cb(null, x.role.name === 'teamleader' && x.groupid === context.modelid); }
Comments
Post a Comment