'register request doesnt take the argument i only get id as an argumant

enter image description here

*i dont know why

register request doesnt take the argument i only get id as an argumant

 router.post("/register",async (req, res) => {

  User.findOne({ Username: req.body.Username }).then(user => {
    if (user) {
      return res.status(400).json({ Username: "Email already exists" });
    } else {
     
      
      const newUser = new User({
        Sass:req.body.Sass, 
        Matemp: req.body.Matemp,
        Nom:req.body.Nom,
        Cin:req.body.Cin,
        Prenom:req.body.Prenom,
        StartDate:req.body.StartDate
       ,Departement:req.body.Departement
       ,Username:req.body.Username
       ,Password:req.body.Password
      });
      newUser.save()
      console.log("done");
      return res.status(200).json({msg: newUser});
   
   
      }
    
  });
});

this is my user model

*i used mongooose i had this problem for 2 weeks now and i dont know whats wrong * const mongoose = require("mongoose"); const Schema = mongoose.Schema;

// Create Schema
const UserSchema = new Schema({
  Sass: {
      Type:String 
     
  },
  Matemp : {
      Type : String 
      
  },
  Nom : {
      Type : String 
      
  },
  Cin : {
      Type : String 
    
  },
  Prenom : {
      Type : String 
      
  },
  StartDate : {
      Type : Date
     
  }, 
  Departement: {
      Type : String
     
  },
  Username : {
      Type : String
      
  },
 Password: {
      Type : String
      
  },
});


module.exports = User = mongoose.model("users", UserSchema);


Solution 1:[1]

I'm not sure of what your User variable is but I have my User as:

 const User = connection.models.user;

and I also added the await to wait for the mongo function to return

router.post("/register", async(req, res) => {

  const user = await User.findOne({
    Username: req.body.Username
  }).then(user => {
    if (user) {
      return res.status(400).json({
        Username: "Email already exists"
      });
    } else {


      const newUser = = await User.create({
        Sass: req.body.Sass,
        Matemp: req.body.Matemp,
        Nom: req.body.Nom,
        Cin: req.body.Cin,
        Prenom: req.body.Prenom,
        StartDate: req.body.StartDate,
        Departement: req.body.Departement,
        Username: req.body.Username,
        Password: req.body.Password
      });
      newUser.save()
      console.log("done");
      return res.status(200).json({
        msg: newUser
      });
    }

  });
});

I also worked on it some more and here is the refactored function:

// Register a new User
router.post('/register', createUser);


async function createUser(req, res) {
  try {
    const user = await User.findOne({
      Username: req.body.Username
    });
    if (user) {
      res.status(400).json({
        success: false,
        message: "Email Already Exist"
      });
      return;
    }
    const data = await User.create({
      Sass: req.body.Sass,
      Matemp: req.body.Matemp,
      Nom: req.body.Nom,
      Cin: req.body.Cin,
      Prenom: req.body.Prenom,
      StartDate: req.body.StartDate,
      Departement: req.body.Departement,
      Username: req.body.Username,
      Password: req.body.Password
    });
    const newUser = await User.findById(data._id);
    res.json({
      success: true,
      message: "user details",
      data: newUser,
    });
  } catch (error) {
    console.error(error);
    res.json({
      success: false,
      message: "user not found",
      error
    });
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Xanik