Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
///backend code first

///User Schema

JavaScript
const UserSchema = new mongoose.Schema(
  {
    username: { type: String, required: true, unique: true },
    name: { type: String },
    email: { type: String, required: true },
    mobileNo: { type: Number, required: true, unique: true },
    password: { type: String, required: true },
    address: [
      {
        locality: { type: Number, unique: true },
        pincode: { type: Number, unique: true },
        city: { type: String, unique: true },
        state: { type: String, unique: true },
        country: {
          type: String,
          unique: true,
        },
        desc: {
          type: String
        },
      },
    ],
    isAdmin: {
      type: Boolean,
      default: false,
    },
  },
  { timestamps: true }
);

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



/// index file

JavaScript
app.use(cors());
app.use(express.json());


///now frontend code

//axios instance

JavaScript
export default axios.create({
  baseURL: "http://localhost:5000",
  headers: {
    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
    "Access-Control-Allow-Origin": "*",
  },
});


///api call

JavaScript
export const register = (correctInput) => {
  console.log(correctInput);
  axios
    .post("/api/auth/register", correctInput)
    .then((res) => console.log(res.data))
    .catch((err) => console.log(err));
};


///signup js

JavaScript
const SignUp = () => {
  const [input, setInput] = useState([]);
  const Navigate = useNavigate();
  const [passValidate, setPassvelidate] = useState(true);

  const handleInputChange = (e) => {
    setInput((prevState) => ({
      ...prevState,
      [e.target.name]: e.target.value,
    }));
  };

  const handleRegisterClick = () => {
    if (input.password === input.passAgain) {
      const { passAgain, ...correctInput } = input;
      register(correctInput);
    } else {
      setPassvelidate(false);
    }
  };

  return (
    <div className={styles.signUp}>
      <h1 className={styles.head}>
        send<span>O</span>
      </h1>
      <div className={styles.formWrapper}>
        <h1>Create account</h1>
        <label htmlFor="name">Your name</label>
        <input
          type="text"
          name="username"
          onChange={handleInputChange}
          placeholder="name"
        />
        <label htmlFor="mobile">Mobile number</label>
        <input
          type="text"
          name="mobileNo"
          placeholder="mobile"
          onChange={handleInputChange}
        />
        <label htmlFor="password">email</label>
        <input
          type="text"
          name="email"
          placeholder="email"
          onChange={handleInputChange}
        />
        <small>{passValidate ? "" : "password does not match"}</small>
        <label htmlFor="password">Password</label>
        <input
          type="password"
          name="password"
          placeholder="password"
          onChange={handleInputChange}
        />
        <label htmlFor="passAgain">Password again</label>
        <input
          type="password"
          name="passAgain"
          placeholder="Enter Your Password Again..."
          onChange={handleInputChange}
        />
        <button onClick={handleRegisterClick}>Continue</button>
      </div>
      <p className={styles.para}>
        By creating an account or logging in , you agree to Picko's Condition of
        Use and Privacy Policy.
      </p>
      <p className={styles.para}>
        Already have an account?{" "}
        <span className={styles.signinLink} onClick={() => Navigate("/signin")}>
          Sign in
        </span>
      </p>
    </div>
  );
};

export default SignUp;


///This is the error

PowerShell
Backend Server running on http://localhost:5000
DB Connection successful
Error: User validation failed: username: Path `username` is required., email: Path `email` is required., mobileNo: Path `mobileNo` is required.
    at ValidationError.inspect (F:\PICKO\PICOMERN\picko-backend\node_modules\mongoose\lib\error\validation.js:48:26)
    at formatValue (node:internal/util/inspect:763:19)
    at inspect (node:internal/util/inspect:340:10)
    at formatWithOptionsInternal (node:internal/util/inspect:2006:40)       
    at formatWithOptions (node:internal/util/inspect:1888:10)
    at console.value (node:internal/console/constructor:323:14)
    at console.log (node:internal/console/constructor:359:61)
    at F:\PICKO\PICOMERN\picko-backend\routes\auth.js:22:13
    at processTicksAndRejections (node:internal/process/task_queues:96:5) { 
  errors: {
    username: ValidatorError: Path `username` is required.
        at validate (F:\PICKO\PICOMERN\picko-backend\node_modules\mongoose\lib\schematype.js:1323:13)
        at SchemaString.SchemaType.doValidate (F:\PICKO\PICOMERN\picko-backend\node_modules\mongoose\lib\schematype.js:1307:7)
        at F:\PICKO\PICOMERN\picko-backend\node_modules\mongoose\lib\document.js:2757:18
        at processTicksAndRejections (node:internal/process/task_queues:78:11) {
      properties: [Object],
      kind: 'required',
      path: 'username',
      value: undefined,
      reason: undefined,
      [Symbol(mongoose:validatorError)]: true
    },
    email: ValidatorError: Path `email` is required.
        at validate (F:\PICKO\PICOMERN\picko-backend\node_modules\mongoose\lib\schematype.js:1323:13)
        at SchemaString.SchemaType.doValidate (F:\PICKO\PICOMERN\picko-backend\node_modules\mongoose\lib\schematype.js:1307:7)
        at F:\PICKO\PICOMERN\picko-backend\node_modules\mongoose\lib\document.js:2757:18
        at processTicksAndRejections (node:internal/process/task_queues:78:11) {
      properties: [Object],
      kind: 'required',
      path: 'email',
      value: undefined,
      reason: undefined,
      [Symbol(mongoose:validatorError)]: true
    },
    mobileNo: ValidatorError: Path `mobileNo` is required.
        at validate (F:\PICKO\PICOMERN\picko-backend\node_modules\mongoose\lib\schematype.js:1323:13)
        at SchemaNumber.SchemaType.doValidate (F:\PICKO\PICOMERN\picko-backend\node_modules\mongoose\lib\schematype.js:1307:7)
        at F:\PICKO\PICOMERN\picko-backend\node_modules\mongoose\lib\documen
PS F:\PICKO\PICOMERN\picko-backend> npm start

> picko-backend@1.0.0 start
> node index.js

Backend Server running on http://localhost:5000
DB Connection successful
MongoServerError: E11000 duplicate key error collection: picko.users index: 
address.locality_1 dup key: { address.locality: null }
    at F:\PICKO\PICOMERN\picko-backend\node_modules\mongodb\lib\operations\insert.js:51:33
    at F:\PICKO\PICOMERN\picko-backend\node_modules\mongodb\lib\cmap\connection_pool.js:273:25
    at handleOperationResult (F:\PICKO\PICOMERN\picko-backend\node_modules\mongodb\lib\sdam\server.js:363:9)
    at MessageStream.messageHandler (F:\PICKO\PICOMERN\picko-backend\node_modules\mongodb\lib\cmap\connection.js:474:9)
    at MessageStream.emit (node:events:390:28)
    at processIncomingData (F:\PICKO\PICOMERN\picko-backend\node_modules\mongodb\lib\cmap\message_stream.js:108:16)
    at MessageStream._write (F:\PICKO\PICOMERN\picko-backend\node_modules\mongodb\lib\cmap\message_stream.js:28:9)
    at writeOrBuffer (node:internal/streams/writable:389:12)
    at _write (node:internal/streams/writable:330:10)
    at MessageStream.Writable.write (node:internal/streams/writable:334:10) 
{
  index: 0,
  code: 11000,
  keyPattern: { 'address.locality': 1 },
  keyValue: { 'address.locality': null }
}


What I have tried:

///Its been two days but i am not getting any results .i have banned from asking questions from stackoverflow . i am stressed and depressed
Posted
Updated 16-Apr-22 22:03pm
Comments
the headless nick 20-Apr-22 7:41am    
Did the solution below help?

1 solution

const [input, setInput] = useState([]);
change this code like this.
const [input, setInput] = useState({});
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900