400 Bad Request in MERN Project, solution not found anywhere

Hey! I’m doing this course on Udemy: MERN Stack React Node Ecommerce from Scratch to Deployment. Still in the initial phase. This is my User schema model:

const mongoose = require('mongoose');
const crypto = require('crypto');
const { v1: uuidv1 } = require('uuid');

const userSchema = new mongoose.Schema(
    {
        name: {
            type: String,
            trim: true,
            required: true,
            maxlength: 32
        },
        email: {
            type: String,
            trim: true,
            required: true,
            unique: true
        },
        hashed_password: {
            type: String,
            required: true
        },
        about: {
            type: String,
            trim: true
        },
        salt: String,
        role: {
            type: Number,
            default: 0
        },
        history: {
            type: Array,
            default: []
        }
    },
    { timestamps: true }
);

// virtual field for hashing pswd
userSchema
    .virtual('password')
    .set(function(password) {
        this._password = password;
        this.salt = uuidv1();
        this.hashed_password = this.encryptPassword(password);
    })
    .get(function() {
        return this._password;
    });

userSchema.methods = {
    authenticate: function(plainText) {
        return this.encryptPassword(plainText) === this.hashed_password;
    },

    encryptPassword: function(password) {
        if (!password) return '';
        try {
            return crypto
                .createHmac('sha1', this.salt)
                .update(password)
                .digest('hex');
        } catch (err) {
            return '';
        }
    }
};

module.exports = mongoose.model('User', userSchema);

Giving GET request is working fine, however POST is giving 400 Bad Request.

Acc to me, the syntax is correct. Then why the error?
(Using Intellij IDE on Linux.)

Thanks in advance!

PS: I’m new to MERN etc.

Hello there,
Welcome to the community,

Please check this thread to resolve your issue.
and use lowercase H in localhost of your URL.

If you have any other questions, feel free to ask.
Best Regards,
Rishi Purwar

Hey Rishi
None of the suggestions given in that post seem to be working for me.

Please share the ss of your headers and params

Currently I’m checking only one field. But 404 persists even after checking other fields.

please share the ss of params

It’s empty

Also Authorization is at NO AUTH.

have you tried this?
and don’t uncheck any of the default headers and try again with the correct api url

create a new tab then try to run this signup request again with above mentioned content-type header

1 Like

It worked! Thanks a lottt!

1 Like

Keep up the good work👍

1 Like