JS对象尝试访问Loopback相关的模型查询时出现奇怪的行为

我正在使用Loopback Framework,做一个web项目。 但是我认为我在这里公开的这个问题与这个问题有关,但是有一般的Javascript / Node.JS知识。

在代码的一部分,我正在做:

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); } } }); 

确定与此,但它尝试比较roles[i].role.name 。 所以,我去logging什么roles[i]对象包含。

  { 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 } } 

好吧,没有错,但它仍然失败,所以我试图打印只是role属性。 令我惊讶的是:

 { [Function] update: [Function], destroy: [Function], create: [Function], build: [Function], _targetClass: 'Role' } 

那么, role属性似乎是某种function? 但是之前是如何正确打印的呢?

最终,失去了我的挫折,我试着var role = JSON.parse(JSON.stringify(roles[i]));

然后,我可以正常访问对象的每个属性,但这不是干净的,也不正常的。

这让我头脑里多年来JS编程第一次(虽然是业余的),如果有人能向我澄清这一点,我会很高兴。 谢谢

编辑:它似乎是具体到这个框架,所以我改变标题,以帮助社区。

我刚刚发现问题1425链接到以下文档 :

使用Node.js API,您需要调用toJSON()将返回的模型实例与相关项目转换为普通的JSON对象

请注意关系属性指向关系方法的JavaScript 函数

所以看来你必须使用

 for (var i=0; i<roles.length; i++) { var x = roles[i].toJSON(); cb(null, x.role.name === 'teamLeader' && x.groupId === context.modelId); }