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

18 messages · Page 1 of 1 (latest)

sage coral
#

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
```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

subtle fieldBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

jade goblet
#

@jade goblet on github

proud escarpBOT
narrow meteor
#

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.

sage coral
sage coral
sage coral
#

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

narrow meteor
sage coral
#

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

jade goblet
#

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

sage coral
sage coral
#

these are my logs