DEV Community

sachin sakhare
sachin sakhare

Posted on

How to solve Node API

exports.addUser = async (req,res) => {
    try {
        const newUser = new User(req.body);
        const savedUser = await newUser.save();
        res.status(201).json(savedUser)
    } catch (error) {
        res.status(400).json({error:error.message})
    }
}
Enter fullscreen mode Exit fullscreen mode
const mongoose = require('mongoose');

const userSchema = mongoose.Schema({
    id: { type: Number, required: true, unique: true },
    name: { type: String, required: true, trim: true },
    username: { type: String, required: true, unique: true },
    email: { type: String, required: true, unique: true },
    gender: { type: String, required: true, enum: ['Male', 'Female', 'Other'] },
    mobileno: { type: Number, required: true },
    dob: { type: Date, required: true },
    address: {
        street: { type: String, trim: true },
        city: { type: String, required: true, trim: true },
        zipcode: { type: String, required: true, trim: true },
        geo: {
            lat: { type: String },
            lng: { type: String } // Corrected from "lang" to "lng"
        }
    }
});

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

Enter fullscreen mode Exit fullscreen mode

I am passing on postman agains api
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"gender": "Male",
"mobileno": 9944990044,
"dob": "1990-03-12",
"address": {
"street": "Kulas Light",
"city": "Gwenborough",
"zipcode": "413874",
"geo": {
"lat": "-373159",
"lng": "811496"
}
}
}

Top comments (0)