1

following are my code , It is running well if , I find from database but its only saving IDs into database on create method

const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const app = express();


// create application/json parser
app.use(bodyParser.json())

// create application/x-www-form-urlencoded parser
app.use(bodyParser.urlencoded({ extended: false }))

mongoose.connect('mongodb://localhost/quicks');
const db = mongoose.connection;

const userSchema = mongoose.Schema({
    name: String
});
const User = mongoose.model('user', userSchema);


app.get('/', (req, res) => res.send('Hello World!'))

app.post('/createuser', (req, res) => {
    let value = {
        name : req.body.Name,
        email : req.body.Email,
        password : req.body.Password
    };
    User.create(value)
    .then((records) => {return res.json(records)})
    .catch((err) => {return res.json("err")})
})


app.post('/user', (req, res) => {
    let value = {
        _id : req.body.ID
    };
    user.findOne(value)
    .then((records) => {return res.json(records)})
    .catch((err) => {return res.json("err")})
})


app.listen(4000, () => console.log('Example app listening on port 4000!'))

i am not able to get the data which i have created . Only the ID is created. what would be the possible error?

1 Answer 1

1

In your schema email and password fields are missing.

try this,

app.post('/createuser', (req, res) => {
    let value = {
        name: req.body.Name,
        email: req.body.Email,
        password: req.body.Password
    };
    let model = new User();
    model = Object.assign(model, value);
    model.save()
        .then((records) => {
            return res.json(records)
        })
        .catch((err) => {
            return res.json("err")
        })
})
Sign up to request clarification or add additional context in comments.

3 Comments

Its working with this, Thank You! But I have been doing this to all of my previous code . which worked well with if I required model/schema from other file. Can you please tell me . what was wrong with my code?
I'm not sure how to create works, I always use save function.
Thank YOU ! Anyways . Its my fault . That code was also working but I was putting name in postman instead of Name. Anyways . Closing this thread

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.