admin管理员组文章数量:1406950
I am working with the Loopback Framework, doing a web project. But I think that the question that I am exposing here has less to do with this, but with general Javascript / Node.JS knowledge.
At one part of the code, I am doing:
roleMapping.find({
where: {
principalType: 'USER',
principalId: context.principals[0].id
},
include: 'role'
}, function(err, roles){
console.log(roles[0]);
for (var i in roles)
{
if (roles[i].role.name === 'teamLeader' &&
roles[i].groupId === context.modelId)
{
cb(null,true);
}else {
cb(null,false);
}
}
});
Ok with this, but it fails when trying to pare roles[i].role.name
.
So, I went logging what the roles[i]
object contained.
{ groupId: 1,
id: 3,
principalType: 'USER',
principalId: 1,
roleId: 2,
role:
{ id: 2,
name: 'teamLeader',
description: 'The leader(s) of a team',
created: null,
modified: null } }
Ok, nothing wrong, but it still fails, so I tried to print just the role
property. And to my surprise:
{ [Function]
update: [Function],
destroy: [Function],
create: [Function],
build: [Function],
_targetClass: 'Role' }
So, the role
property seems to be some sort of function? But how it was been correctly printed before?
Eventually, lost in my frustration I tried var role = JSON.parse(JSON.stringify(roles[i]));
And then I could access every property of the object normally, but this is not clean nor normal.
This blew my mind for the first time in years of JS programming (sort of amateurish though), and I would be pleased if someone could clarify this to me. Thanks
EDIT: It seems that it is specific to this Framework, so I'm changing title to help munity.
I am working with the Loopback Framework, doing a web project. But I think that the question that I am exposing here has less to do with this, but with general Javascript / Node.JS knowledge.
At one part of the code, I am doing:
roleMapping.find({
where: {
principalType: 'USER',
principalId: context.principals[0].id
},
include: 'role'
}, function(err, roles){
console.log(roles[0]);
for (var i in roles)
{
if (roles[i].role.name === 'teamLeader' &&
roles[i].groupId === context.modelId)
{
cb(null,true);
}else {
cb(null,false);
}
}
});
Ok with this, but it fails when trying to pare roles[i].role.name
.
So, I went logging what the roles[i]
object contained.
{ groupId: 1,
id: 3,
principalType: 'USER',
principalId: 1,
roleId: 2,
role:
{ id: 2,
name: 'teamLeader',
description: 'The leader(s) of a team',
created: null,
modified: null } }
Ok, nothing wrong, but it still fails, so I tried to print just the role
property. And to my surprise:
{ [Function]
update: [Function],
destroy: [Function],
create: [Function],
build: [Function],
_targetClass: 'Role' }
So, the role
property seems to be some sort of function? But how it was been correctly printed before?
Eventually, lost in my frustration I tried var role = JSON.parse(JSON.stringify(roles[i]));
And then I could access every property of the object normally, but this is not clean nor normal.
This blew my mind for the first time in years of JS programming (sort of amateurish though), and I would be pleased if someone could clarify this to me. Thanks
EDIT: It seems that it is specific to this Framework, so I'm changing title to help munity.
Share edited Jun 19, 2015 at 11:31 Dark_eye asked Jun 18, 2015 at 22:49 Dark_eyeDark_eye 4348 silver badges15 bronze badges 8- Can you show us the code you used to print them? – Bergi Commented Jun 18, 2015 at 22:58
-
console.log(roles[i]);
printing whole object ok, andconsole.log(roles[i].role)
printing the weird thing. – Dark_eye Commented Jun 18, 2015 at 23:01 -
I think I've seen mongodb doing similar weird things. Does
roles[i]
have a.toJSON
method? – Bergi Commented Jun 18, 2015 at 23:15 -
Yeah it looks like you need to extract the actual value (your object) somehow. It also looks like the logged object might in fact be a function. Does
roles[i]()
return anything? – Jan Commented Jun 18, 2015 at 23:19 -
Thay may have something to do with the behaviour of the
for (... in ...)
loop. You could try usingfor (var i = 0, length = roles.length; i < length; i++) { <your code here> }
instead. See developer.mozilla/en/docs/Web/JavaScript/Reference/… – reg4in Commented Jun 18, 2015 at 23:20
1 Answer
Reset to default 10I just found issue 1425 which links to the following docs:
With Node.js API, you need to call
toJSON()
to convert the returned model instance with related items into a plain JSON objectPlease note the relation properties […] points to a JavaScript function for the relation method.
So it seems you have to use
for (var i=0; i<roles.length; i++) {
var x = roles[i].toJSON();
cb(null, x.role.name === 'teamLeader'
&& x.groupId === context.modelId);
}
本文标签: javascriptJS Object strange behaviour when trying access Loopback related model queryStack Overflow
版权声明:本文标题:javascript - JS Object strange behaviour when trying access Loopback related model query - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744935673a2633166.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论