Javascript 检索子级对象的属性
我的代码如下。我的目标是从下面给出的函数对象中检索学生的名字。我确实尝试了JSON.Stringify和JSON.ParseJavascript 检索子级对象的属性,javascript,json,node.js,loopback,Javascript,Json,Node.js,Loopback,我的代码如下。我的目标是从下面给出的函数对象中检索学生的名字。我确实尝试了JSON.Stringify和JSON.Parse EnrolledSessions.find({ where: { aproval_status: 'pending', tutor_id: tutorId }, include: { relation:'StudentRelation' } }, function (err, sessions
EnrolledSessions.find({
where: {
aproval_status: 'pending',
tutor_id: tutorId
},
include: {
relation:'StudentRelation'
}
},
function (err, sessions) {
if (err) cb(err);
if (!err) {
for(var i in sessions) {
var item = sessions[i];
console.log(item.StudentRelation);
}
}
});
下面是console.log(item.StudentRelation)的结果代码>从上面开始
{ [Function: bound ]
getAsync: [Function: bound ]
update: [Function: bound ],
destroy: [Function: bound ],
create: [Function: bound ],
build: [Function: bound ],
_targetClass: 'erudoUser' }
{ [Function: bound ]
getAsync: [Function: bound ]
update: [Function: bound ],
destroy: [Function: bound ],
create: [Function: bound ],
build: [Function: bound ],
_targetClass: 'erudoUser' }
{ [Function: bound ]
getAsync: [Function: bound ]
update: [Function: bound ],
destroy: [Function: bound ],
create: [Function: bound ],
build: [Function: bound ],
_targetClass: 'erudoUser' }
{ [Function: bound ]
getAsync: [Function: bound ]
update: [Function: bound ],
destroy: [Function: bound ],
create: [Function: bound ],
build: [Function: bound ],
_targetClass: 'erudoUser' }
我试图循环通过的函数对象
[ { coursesession_id: 574fc6baa09d9bc4050b974e,
student_id: 574fbf10179281fc15c5db37,
tutor_id: 574fbff2179281fc15c5db3d,
aproval_status: 'pending',
start_date: Thu Jun 02 2016 11:00:00 GMT+0530 (India Standard Time),
duration: '06:00',
session_type: 'One To One',
enteredtopic_name: 'addition',
payment_status: 'paid',
course_completed: false,
TxnId: 'IKGqrpI6IHnxIFEJsFt53EIqw',
id: 574fc6baa09d9bc4050b974f,
StudentRelation:
{ user_type: 'student',
firstname: 'Student',
lastname: 'Ps',
active_status: true,
email: 'sikha.p91@gmail.com',
grade: '574fbeb2179281fc15c5db34',
syllabus: '574fbea9179281fc15c5db33',
mobile: '8157897518',
place: 'kannru',
password: '$2a$10$YIUVCclA6.vBeePZuMY7uu.6lYr..fQD4o9dSY7uF9FmgkTWAqq3i',
emailVerified: true,
id: 574fbf10179281fc15c5db37 } },
{ coursesession_id: 574fde1dc59f447c1235007b,
student_id: 574fbf10179281fc15c5db37,
tutor_id: 574fbff2179281fc15c5db3d,
aproval_status: 'pending',
start_date: Mon Jun 13 2016 09:00:00 GMT+0530 (India Standard Time),
duration: '01:00',
session_type: 'One To One',
enteredtopic_name: 'integration',
payment_status: 'paid',
course_completed: false,
TxnId: 'KpDGHtLwIK6D3zIICtIGoL46p',
id: 574fde1dc59f447c1235007c,
StudentRelation:
{ user_type: 'student',
firstname: 'Student',
lastname: 'Ps',
active_status: true,
email: 'sikha.p91@gmail.com',
grade: '574fbeb2179281fc15c5db34',
syllabus: '574fbea9179281fc15c5db33',
mobile: '8157897518',
place: 'kannru',
password: '$2a$10$YIUVCclA6.vBeePZuMY7uu.6lYr..fQD4o9dSY7uF9FmgkTWAqq3i',
emailVerified: true,
id: 574fbf10179281fc15c5db37 } },
{ coursesession_id: 57511d25388d2150161559de,
student_id: 574fbf10179281fc15c5db37,
tutor_id: 574fbff2179281fc15c5db3d,
aproval_status: 'pending',
start_date: Fri Jun 03 2016 09:00:00 GMT+0530 (India Standard Time),
duration: '06:00',
session_type: 'One To One',
enteredtopic_name: 'multiplication',
payment_status: 'paid',
course_completed: false,
TxnId: 'voyn9oqyG14ItzzwqHI4GxM1o',
id: 57511d25388d2150161559df,
StudentRelation:
{ user_type: 'student',
firstname: 'Student',
lastname: 'Ps',
active_status: true,
email: 'sikha.p91@gmail.com',
grade: '574fbeb2179281fc15c5db34',
syllabus: '574fbea9179281fc15c5db33',
mobile: '8157897518',
place: 'kannru',
password: '$2a$10$YIUVCclA6.vBeePZuMY7uu.6lYr..fQD4o9dSY7uF9FmgkTWAqq3i',
emailVerified: true,
id: 574fbf10179281fc15c5db37 } },
{ coursesession_id: 57511d25388d2150161559de,
student_id: 574fbf10179281fc15c5db37,
tutor_id: 574fbff2179281fc15c5db3d,
aproval_status: 'pending',
start_date: Sat Jun 04 2016 09:00:00 GMT+0530 (India Standard Time),
duration: '06:00',
session_type: 'One To One',
enteredtopic_name: 'multiplication',
payment_status: 'paid',
course_completed: false,
TxnId: 'voyn9oqyG14ItzzwqHI4GxM1o',
id: 57511d25388d2150161559e0,
StudentRelation:
{ user_type: 'student',
firstname: 'Student',
lastname: 'Ps',
active_status: true,
email: 'sikha.p91@gmail.com',
grade: '574fbeb2179281fc15c5db34',
syllabus: '574fbea9179281fc15c5db33',
mobile: '8157897518',
place: 'kannru',
password: '$2a$10$YIUVCclA6.vBeePZuMY7uu.6lYr..fQD4o9dSY7uF9FmgkTWAqq3i',
emailVerified: true,
id: 574fbf10179281fc15c5db37 } } ]
您必须使用将返回的模型实例及其相关项转换为普通的JSON对象。上面的Stringfy答案将起作用,但这是一种攻击
这是正确的回送方式
let info = item.getAsync().then((res) => cb(null,res));
打印console.log(会话)时会得到什么;我也有同样的问题,我最终不得不完全按照伊万的建议去做,而且效果很好。我对这个解决方案不太满意,因为它可以进行大量的中间数据处理,但至少它是有效的。