Nodejs mongoose

I am unable to create new user or send data to database.

function to create new user

const createUser = async (req, res) => {
  const { id, name, email, gender, status } = req.body
  try {
    const newUser = await User.create({
    // const savedUser = await
  } catch (error) {

User Model

const mongoose = require('mongoose')

const UserSchema = new mongoose.Schema(
    id: {
      type: Number,
      required: true,
      unique: true,
    name: {
      type: String,
      required: true,
      min: 3,
      max: 50,
    email: {
      type: String,
      required: true,
      unique: true,
    gender: {
      type: String,
      required: true,
    status: {
      type: String,
      required: true,
  { timestamp: true }

module.export = mongoose.model('User', UserSchema)

@bappyasif please help on this problem its very urgent as this project i need to submit by tomorrow.

I don’t know how anyone is supposed to help you with just what you have posted.

Post the repo and explain the problem in more detail.

Github repo : GitHub - iprime2/goldstone-api
The api not working can check it.

Please put a little more effort into your question than that. Provide us with as much information as you can.

Side note, you are not supposed to commit the .env file. It has your DB credentials, remove it from Git and create new credentials.

The model code is not right.

It is module.exports (s) and you are creating a named export but using it as a default in the import.

// export
module.exports = { User };
// import
const { User } = require("../model/User");

Edit: Before you just fix it try actually logging the error in the try/catch and see if you can debug this and if the solution makes sense based on the debugging.

As an aside, I’m not so sure 400 is the correct status code here. I think for the try/catch it should be 500 and then if you do any validation of the body data it can send 400 codes.

I am have changed the code as you have suggested but it still not work the data is not being uploaded to database.

No you didn’t.

module.export = { User }

It is exports not export

Thanks @lasjorg for help !!

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