Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

in these functions error message is not showing in runtime

const Course = require("../models/course.model");

In here code is usually working but when it facing an error it wont show proper error message in run time also it didnt show error in the console

const addCourse = async(ctx) => {    
   try {
    console.log(ctx.request.body);
    const {courseName, courseFee, students} = ctx.request.body;     
    const course = await Course.create({
        courseName,
          courseFee,
            students}) 

    // return (ctx.body = {course: course});
    return (ctx.body = course);
} catch (error) {
    return (ctx.body ={ message : message})
}

also in the get function is working but when error comes out it didn’t
show like in below code

const getCourses = async(ctx) =>{
    try {
    const courses = await Course.find({}).populate({
            path: "students",
            select : "name nic",
        })
        // return (ctx.body ={courses: courses})        
        return (ctx.body =courses) 
    } catch (error) {
        return (ctx.body = { message : message})

    }
} 

same thing happened

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 const updateCourse = async(ctx)=>{
    try {
        const courseId = ctx.params.id;

        const {courseName,courseFee,students} = ctx.request.body;
        const course = await Course.findByIdAndUpdate(courseId,{courseName,courseFee,students})
        return (ctx.body = course);
   } catch (error) {
        return (ctx.body ={ message : error.message})
    }
}

>Solution :

In all of these functions you haven’t return the error message
properly

return (ctx.body ={ message : error.message})

hope this helps

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading