Node.JS Express Problems With GET Route With Two MongoDB Queries Not Finding Document

121
April 29, 2019, at 12:10 PM

One of the routes in my Node.JS web app is supposed to query a MongoDB and return a user and a lesson, find the user's progress for the lesson, and then render a page from a template using EJS. It seems to go and fetch the information correctly, but then, for some reason, it seems to loop back through and not be able to find the lesson. The program then hangs, waiting for a response. What is going on here? I am not sure if this is some problem caused by the asynchronous nature of the mongo queries. Any information would be helpful. Thanks!

app.get('/lessons/:lesson', (req, res) => {
    if(req.isAuthenticated()){
        console.log('User authenticated');
        const paramLesson = req.params.lesson;
        User.findById(req.user.id, function (err, foundUser) {
            if(err){
                console.log(err);
            }
            if (foundUser){
                console.log("User found: " + foundUser.id);
                Lesson.findOne({ code: paramLesson }, (err, foundLesson) => {
                    if(err){
                        console.log(err);
                    }
                    if(foundLesson) {
                        console.log('Lesson found: ' + foundLesson.code);
                        // Check all progress to see if there is a value for this lesson, if not create a document
                        progress = foundUser.progress.find( (prog) => {
                            return prog.lessonId === foundLesson.code;
                        })
                        console.log('Progress found: ' + progress.currentProgress);
                        if(progress){
                            console.log('Rendering lesson...');
                            return res.render('example', {lesson: foundLesson, user: foundUser, progress: progress.currentProgress});
                        } else { // Create progress document for user if none exists for this lesson
                            console.log("Inserting lesson progress");
                            // object to insert for current lesson
                            let lessonProgress = {
                                lessonId: paramLesson,
                                currentProgress: 0,
                                complete: false
                            };
                            foundUser.progress.push(lessonProgress);
                            foundUser.save();
                            console.log('redirecting to /lessons/' + paramLesson);
                            return res.redirect("/lessons/" + paramLesson);
                        }
                    } else {
                        console.log('No lesson found!');
                    }
                });     
            } else {
                console.log('No user found!');
            }
        });  
    } else {
        console.log('User not authenticated');
        return res.redirect('/login');
    }  
});
Rent Charter Buses Company
READ ALSO
In Express JS does app.listen need to be at the end of the file?

In Express JS does app.listen need to be at the end of the file?

In most tutorials using Express JS the applisten function is always added at the end of the file

145
ESP8266 / Arduino modbus RTU Buffer data conversion

ESP8266 / Arduino modbus RTU Buffer data conversion

I am using ESP8266 and ModbusMasterh library to communicate with RS485 enabled power meter

128
Easy to implement payment gateway that works in Pakistan?

Easy to implement payment gateway that works in Pakistan?

I am a beginner in front end development and I need help regarding payment gateway easiest to implement

127