@@ -45,13 +45,20 @@ export const setLessons = payload => {
4545 } ;
4646} ;
4747
48+ // A valid MongoDB ObjectId is a 24-character hex string. Lesson author/project
49+ // fields sometimes hold display names (e.g. "James", "project 1") instead of
50+ // real references; skip those so we don't fire doomed 404/500/400 requests.
51+ const isObjectId = id => typeof id === 'string' && / ^ [ 0 - 9 a - f A - F ] { 24 } $ / . test ( id ) ;
52+
4853export const fetchBMLessons = ( ) => {
4954 return async dispatch => {
5055 try {
5156 const response = await axios . get ( ENDPOINTS . BM_LESSONS ) ;
5257 const lessons = response . data ;
53- const authorIds = [ ...new Set ( lessons . map ( lesson => lesson . author ) ) ] ;
54- const projectIds = [ ...new Set ( lessons . map ( lesson => lesson . relatedProject ) ) ] ;
58+ const authorIds = [ ...new Set ( lessons . map ( lesson => lesson . author ) . filter ( isObjectId ) ) ] ;
59+ const projectIds = [
60+ ...new Set ( lessons . map ( lesson => lesson . relatedProject ) . filter ( isObjectId ) ) ,
61+ ] ;
5562
5663 // Keep the more robust approach from honglin-lesson-list-buttons branch
5764 const [ authorProfiles , projectDetails ] = await Promise . all ( [
@@ -129,10 +136,11 @@ export const fetchSingleBMLesson = lessonId => {
129136 const response = await axios . get ( url ) ;
130137 const lesson = response . data ;
131138
132- // Fetch user profile and project details concurrently
139+ // Fetch user profile and project details concurrently, but only when the
140+ // stored values are real ObjectIds (not display names like "project 1").
133141 const [ projectDetails , userProfile ] = await Promise . all ( [
134- dispatch ( fetchProjectById ( lesson . relatedProject ) ) ,
135- dispatch ( getUserProfile ( lesson . author ) ) ,
142+ isObjectId ( lesson . relatedProject ) ? dispatch ( fetchProjectById ( lesson . relatedProject ) ) : null ,
143+ isObjectId ( lesson . author ) ? dispatch ( getUserProfile ( lesson . author ) ) : null ,
136144 ] ) ;
137145
138146 // Update the lesson with author and project details
0 commit comments