0

I want to get a response from a request using my post method into mongoDB my response body keeps loading without any data and my console print out No error which is the default in the switch statement.Any help about the cause and possible solution will help me so much.Thank you

This is my controller

const express=('express');
const asynchandler=require('express-async-handler');
const schema=require('../models/DatabaseSchema');

 const PutUser=asynchandler(async(req,res)=>{
    console.log("Update a post");
});

const postUser=asynchandler(async(req,res)=>{

    const {course,StudentNo}=req.body;

    if(!course || !StudentNo){

        res.status(400);
        throw new Error({message:"No course info"});
    }
    
    const userInfo=await schema.create({
        course,StudentNo
    });

        res.status(200).json(userInfo);  
        res.end("Ending response");  
    
});

module.exports={postUser,getUser,PutUser,DeleteUser}

error handling

const express=('express');
const {AUTHENTICATION_ERROR,SERVER_ERROR}=require('../constant');

const Data=(req,res,err)=>{

    const responsecheck=res.statusCode?res.statusCode:500
    
    switch(responsecheck){
        case AUTHENTICATION_ERROR:
            ({
                title:"Authentication error",
                message:err.message,
            });

            break;
        case SERVER_ERROR:
                ({
                    title:"Server error",
                    message:err.message
                });
                
         default:
            console.log("No error");
        break;      
}
}
module.exports=Data;

This is my models

const mongoose=require("mongoose");

const mongooseDatabase=mongoose.Schema({
course:{
    type:String,
    required:true
},
StudentNo:{
    type:String,
    required:true
}

});
module.exports=mongoose.model("Course1",mongooseDatabase)

This is my server.js

const express=require('express');

const app=express();

const dotenv=require('dotenv').config();

const errorhandle=require('./errorhandling/errorhandler');

const DatabaseConnection=require('./DatabaseConnection/DbConnection');

DatabaseConnection();

const port=process.env.PORT||5000;

app.use(express.json());

app.use(errorhandle);

app.use('/user/userdetails',require("./routes/UserRoutes"));

app.listen(port,()=>{
    console.log('This your new port');
})

This is my route

const express=require('express');
const router=express.Router();

const{getUser,postUser,DeleteUser,PutUser}=require('../controllers/UserController');

router.route('/').post(postUser).get(getUser);
router.route('/:id').put(PutUser).delete(DeleteUser);

module.exports=router;

The server and authorization error in switch statement have a response status of 400 and 401

rickhg12hs
  • 10,638
  • 6
  • 24
  • 42
  • Your error handler needs **4** params... `(err, req, res, next)`. See https://expressjs.com/en/guide/error-handling.html#writing-error-handlers – Phil Jun 09 '23 at 01:12
  • Does this answer your question? [get request not working in Node.JS and express](https://stackoverflow.com/a/71612424/283366) – Phil Jun 09 '23 at 01:14
  • Aha it now works fine but is the next middleware that obligatory and why in this situation – Jaden Appiah Jun 09 '23 at 01:57

0 Answers0