///backend code first
///User Schema
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
app.use(cors());
app.use(express.json());
///now frontend code
//axios instance
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
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
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
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: