2016-06-03 2 views
2

Mein Code ist wie folgt. Mein Ziel ist es, den Vornamen des Schülers aus dem unten angegebenen Funktionsobjekt abzurufen. Ich habe versucht JSON.stringify und JSON.parseAttribute des untergeordneten Objekts abrufen

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

Und hier sind die Ergebnisse für console.log(item.StudentRelation); von oben

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

Funktion Objekt, das ich Schleife versuche durch

[ { 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: '[email protected]', 
      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: '[email protected]', 
      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: '[email protected]', 
      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: '[email protected]', 
      grade: '574fbeb2179281fc15c5db34', 
      syllabus: '574fbea9179281fc15c5db33', 
      mobile: '8157897518', 
      place: 'kannru', 
      password: '$2a$10$YIUVCclA6.vBeePZuMY7uu.6lYr..fQD4o9dSY7uF9FmgkTWAqq3i', 

      emailVerified: true, 
      id: 574fbf10179281fc15c5db37 } } ] 
+0

was Sie erhalten, wenn Sie console.log (Sitzungen) drucken; – Thalaivar

Antwort

2

Sie haben Verwenden Sie toJSON, um die zurückgegebene Modellinstanz mit verwandten Elementen in ein einfaches JSON-Objekt zu konvertieren.

+1

Ich hatte das gleiche Problem, und ich endete damit, genau wie Ivan vorgeschlagen, und es funktioniert. Ich bin nicht sehr glücklich mit der Lösung, da es für eine Menge von Zwischendatenverarbeitung sorgt, aber zumindest funktioniert es. – Jonathan

0

Die obige Antwort von Stringfy wird funktionieren, aber das ist ein Hack.

Dies ist der richtige Weg Loopback

let info = item.getAsync().then((res) => cb(null,res));