Next.js Discord

Discord Forum

Axios Error : 500 (Internal Server Error) in axios api call

Unanswered
Burmese posted this in #help-forum
Open in Discord
BurmeseOP
i am trying to make api (post)request through axios and giving the data to the api from frontend
but the backend is not etting data and its givng me axios error status 500

POST http://localhost:3000/api/generate-course-outline 500 (Internal Server Error)

here is my part in frontend from where i am making api call
 const GenerateCourseOutline=async()=>{
    const courseId=uuidv4();
    //  console.log("formData:", formData);
    //  console.log("createdBy:", user?.primaryEmailAddress?.emailAddress);
      const result=await axios.post('/api/generate-course-outline',{
        courseId:courseId,
        ...formData,
        createdBy:user?.primaryEmailAddress?.emailAddress

      })
      console.log(result);
  }


this is where my api route is made
/api/generate-course-outline
export async function POST(req) {
    try {
      const { courseId, topic, courseType, difficultyLevel, createdBy } = await req.json();
      
    //   generate course layout using ai 
    const PROMPT='Generate a study material for '+topic+' for '+courseType+' and level of difficulty will be '+difficultyLevel+'with the summary of course ,List of Chapters along with summary for each  chapter,topic list in each chapter in JSON format '
    const aiResp=await courseOutlineAIModel.sendMessage(PROMPT)
    const aiResult=JSON.parse(aiResp.response.text());

    // save the result along with user input 
    const dbResult=await db.insert(STUDY_MATERIAL_TABLE).values({
        courseId:courseId,
        courseType:courseType,
        createdBy:createdBy,
        topic:topic,
        courseLayout:aiResult
    }).returning({STUDY_MATERIAL_TABLE})
    console.log(dbResult);
  
      return NextResponse.json({ result: dbResult[0] })
    } catch (error) {
      console.error('Error in POST /api/generate-course-outline:', error);
      return NextResponse.json({ error: 'An error occurred while processing your request' }, { status: 500 });
    }
  }

kindly help me fix tis

15 Replies

@Burmese i am trying to make api (post)request through axios and giving the data to the api from frontend but the backend is not etting data and its givng me axios error status 500 POST http://localhost:3000/api/generate-course-outline 500 (Internal Server Error) here is my part in frontend from where i am making api call javascript const GenerateCourseOutline=async()=>{ const courseId=uuidv4(); // console.log("formData:", formData); // console.log("createdBy:", user?.primaryEmailAddress?.emailAddress); const result=await axios.post('/api/generate-course-outline',{ courseId:courseId, ...formData, createdBy:user?.primaryEmailAddress?.emailAddress }) console.log(result); } this is where my api route is made /api/generate-course-outline javascript export async function POST(req) { try { const { courseId, topic, courseType, difficultyLevel, createdBy } = await req.json(); // generate course layout using ai const PROMPT='Generate a study material for '+topic+' for '+courseType+' and level of difficulty will be '+difficultyLevel+'with the summary of course ,List of Chapters along with summary for each chapter,topic list in each chapter in JSON format ' const aiResp=await courseOutlineAIModel.sendMessage(PROMPT) const aiResult=JSON.parse(aiResp.response.text()); // save the result along with user input const dbResult=await db.insert(STUDY_MATERIAL_TABLE).values({ courseId:courseId, courseType:courseType, createdBy:createdBy, topic:topic, courseLayout:aiResult }).returning({STUDY_MATERIAL_TABLE}) console.log(dbResult); return NextResponse.json({ result: dbResult[0] }) } catch (error) { console.error('Error in POST /api/generate-course-outline:', error); return NextResponse.json({ error: 'An error occurred while processing your request' }, { status: 500 }); } } kindly help me fix tis
West African Lion
can u send or invite me to this repo
@West African Lion on github
Asian black bear
Since it's a 500 you have to check the server logs, in this case the output in your terminal from the dev command to see what error has happened in the route handler.
This will help you narrow down whether your request body handling, the AI request or the DB code is causing issues.
@West African Lion can u send or invite me to this repo
BurmeseOP
i have not deployed it on github . Its in my local machine only .
@Asian black bear Since it's a 500 you have to check the server logs, in this case the output in your terminal from the `dev` command to see what error has happened in the route handler.
BurmeseOP
yeah i have checked the list of data i want to send from frontend ,its rendering there .
but in backend i am not able see the data,also not any changes can i see in db logs
@Burmese yeah i have checked the list of data i want to send from frontend ,its rendering there . but in backend i am not able see the data,also not any changes can i see in db logs
Asian black bear
What I meant is that the dev server is very likely to show the 500 error in the terminal with a stack trace giving you more insight on where and why this is happening.
BurmeseOP
You're right, the dev server's 500 error and stack trace are quite helpful in narrowing down the issue. I have checked the stack trace and it says "error occured while processing your request" when a call was made through api . I m not understanding why is this happening
West African Lion
i understand you didnt deploy
but you can publish your code on github
and i can help you then
the error will be in your vscode as aforementioned by near, not in the browser
BurmeseOP
thanks @West African Lion for your help and support
i have published the repo on github .you can access it from
https://github.com/Sadaf2005/AI-LMS-platform1
BurmeseOP
these are my logs
West African Lion
imma check now @Burmese