Select field and multi select and gender fields not sent to mongodb

I’m building a mern authentification app everything went well except that i have select fields that r not passing to server and obviously the database here s my schema:

import mongoose from "mongoose"
const userSchema =mongoose.Schema({

    Nom:{type:String, required :true},
    Prenom:{type:String, required :true},
    Age: {type:Number,
    },
    Email: {type:String,
        require: true,
        match: /.+\@.+\..+/,
        
    },
    Gender:{type:String,
        default:"Homme"//this is a select menu too
},
    Gouvernorat:{
        type:String,//in the front end the user can choose from a select menu one option
   
},
   /* Cinteret:{type:[Schema.Types.ObjectId],ref:"Categorie"},tried it as a dependent schema but now way*/
   Cinteret:{type:Array,
default:[]},//here it s a multiple select in the front end
    Password:{type:String,require:true},
    confirmPassword:{type:String,require:true},
    Nfollowers:{type:Number,default:0},
    Nfollowing:{type:Number,default:0},
    
}) 


export default  mongoose.model('User',userSchema) 

any help will be appreciated

Hi, I removed your picture. Because you leaked your email and password, and it is better to cover your personal information next time.

The Schema alone won’t help anyone to debug this, have you checked the following?

  • does the frontend store the data correctly in state
  • does the outgoing request from the frontend contain the correct data
  • does the backend receive the correct data
  • does the backend use the right methods to store the data in the database

I’d first of all log the data at every point, and determine where the bug occurs.

1 Like

hi it wasnt real data just fake one anyway thank you

yes the truth i doubt its a problem from the front end because the normal input fields works and i see the sent data in the server and database here s the code of front :
import React ,{useState}from ‘react’

import { Avatar,Button, Paper,Grid, Typography, Container,TextField} from ‘@material-ui/core’

import { useDispatch } from ‘react-redux’;

import { useHistory } from ‘react-router-dom’;

import LockOutlinedIcon from ‘@material-ui/icons/LockOutlined’

import useStyles from ‘./styles’

import Input from “./Input”

import Radio from"./Radio"

import Select1 from “./Select”

import CustomizedHook from “./MultipleSelect”;

import { signin, signup } from ‘…/…/actions/auth’;
const initialState={Nom:"",Prenom:"", Age:"", Gouvernorat:"",Gender:"",Cinteret:"",Email:"", Password:"",confirmPassword:"",Photo:""}

function Auth() {

const [form, setFormData] = useState(initialState);

const [isSignup, setIsSignup] = useState(false);

const dispatch = useDispatch();

const history = useHistory()

const classes = useStyles();

const [ShowPassword, setShowPassword] = useState(false);

const handleShowPassword = () => setShowPassword((prevShowPassword)=>!prevShowPassword)

const switchMode = () => {

setFormData(initialState);

setIsSignup((prevIsSignup) => !prevIsSignup);

setShowPassword(false);

};

const handleSubmit = async (e) => {

e.preventDefault();

if (isSignup) {

  dispatch(signup(form,history));

} else {

  dispatch(signin(form, history));

}

};

    const handleChange=(e)=>{

setFormData({…form,[e.target.name]:e.target.value})

    }

                      

     return (

           

        <Container component="main" maxWidth="xs">

<Paper className={classes.paper} elevation={3}>

<Avatar className={classes.avatar}>

    <LockOutlinedIcon />

</Avatar>

<Typography  component="h1" variant='h6' >{ isSignup ? "inscription" : "connexion"} </Typography>

<form className={classes.form} onSubmit={handleSubmit}>

    <Grid container spacing={2}>

{

    isSignup &&(

        <>

        

        <Input name="Nom" label="Nom" handleChange={handleChange} fullWidth ></Input>

        <Input name="Prenom" label="Prenom" handleChange={handleChange}  fullWidth></Input>

        <Input name="Age" label="age" type="Number" handleChange={handleChange } fullWidth />

       

                  

       < Select1 name="Gouvernorat" fullWidth />/*this a select menu where the user can chosse one option (one state from 24)*/

      <Radio />//this a select menu for gender 

/* this is a multiple select where the user can choose many options*/

        </>

    )

}

<Input name="Password" label="mot de passe" handleChange={handleChange} fullWidth type={ShowPassword ? "text": "password"} handleShowPassword={handleShowPassword}/>

       {isSignup && <Input name="confirmPassword" label="Confirmer mot de passe" handleChange={handleChange} type="password"/>}



    </Grid>

    <Button type="submit" fullWidth variant="contained"  className={classes.submit}>{isSignup ?'Sign up':  "Sign in"}</Button>



 <Grid container justify='flex-end'>
<Button onClick={switchMode}>{isSignup? "already have an account":"register"}</Button>
</form>

</Paper>

)}

export default Auth

Apologies but that is a lot of code, and poorly formatted / almost unreadable so I don’t really know how to help you debug. I can only suggest again that you first check with console.logs on both frontend and backend at which point something’s going wrong with your data.

If you say you “see the sent data in the server”, does that mean that you’re getting all the data you expect, in the format you expect?

ok no problem it s from the front end i identified it and i m trying to debug it thank you :pray:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.