Shahdad Marri
Shahdad Marri

Reputation: 1

Data not posting on mongoose with express and jade

My registration form data is not posting on mongoose database Here is my code

my code working when i get the data from my database but its not posting code please help me guys

    app.set('views', path.join(__dirname, 'views')); 
         app.set('view engine', 'jade');
         app.use(bodyParser.json());
         app.use(express.static(path.join(__dirname, 'public')));
         app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false }));


   
**mongoose connection** 

     mongoose.connect("mongodb://localhost:27017/change")
 
       .then(() => console.log('Connected to MongoDB ...')) 
       .catch(err => console.error('Could not connect to MongoDB....',err));

Schema

   // Schema
    const courseSchema = new mongoose.Schema({
        firstName: String, 
        fName: String,
        add1: String,
        add2: String,
        city: String,
        province: String,
        zip:Number,
        country: String,
        age: Number,
        gender: String,
        color:String,
        Height:Number,
        mTongue:String,
        moi:String,
        disability:String,
        religion:String,
        mHealth:String,
        name:String,
        contact:Number,
        add3:String,
        add4:String,
        city1:String,
        state:String,
        postal:String,
        country1:String,
        RTC:String,
        email: String,
        profileimage:{
            type: String
        },
        date: { type: Date, default: Date.now},
        isPublished: Boolean
    });`

Routing

app.post("/addname",upload.single('profileimage'), (req, res) => {
const myData = new User(req.body);
myData.save()
    .then(item => {
        res.redirect('/');  
    })
    .catch(err => {
        res.status(400).send("Unable to save to database");
    });

**Jade Code **

Here is my registration form code

    > `form(method='post', action='/addname')
    
    
    hr  
    .row
          .col-sm-6
            .form-group
            label Name 
            input.form-control(type='text', name='firstName', placeholder='Enter Name')      
          .col-sm-6
            .form-group
            label Father Name
            input.form-control(type='text', name='fName', placeholder='Enter Name', required)  
          .col-sm-6
            .form-group
            label Address Line 1
            input.form-control(type='text', name='add1', placeholder='Enter Address Line 1', required)      
          .col-sm-6
            .form-group
            label Address Line 2
            input.form-control(type='text', name='add2', placeholder='Enter Address Line 2', required) 
          .col-sm-6
            .form-group
            label City
            input.form-control(type='text', name='city', placeholder='Enter City', required)
          .col-sm-6
            .form-group
            label State/Province/Region
            select.form-control(name='state', id='' .dropdown-menu , required)
              each val in ['Sindh' , 'Punjab' , 'KPK' ,'Balochistan', 'Gilgit-Baltistan' , 'AJK', 'Islamabad']
                option=val         
          .col-sm-6
            .form-group
            label Postal / Zip Code
            input.form-control(type='text', name='zip', placeholder='Enter Postal / Zip Code', required) 
          .col-sm-6
            .form-group
            label Country
            input.form-control(type='text', name='country', placeholder='Country', required)      
          .col-sm-6 
            .form-group
            label Age
            input.form-control(type='number', name='age' , placeholder='Age', required)
          .col-sm-6 
            .form-group
            label Gender
            select.form-control(name='gender', id='' .dropdown-menu , required)
              each val in ['Male' , 'Female' , 'Other']
                option=val   
          .col-sm-6
            .form-group
            label Color
            select.form-control(name='color', id='', required .dropdown-menu)
              each val in ['Fair' , 'Brown' , 'Dark' , 'Black' , 'Wheatish']
                option=val   
          .col-sm-6
            .form-group
            label Height
            input.form-control(type='text', name='height', placeholder='Enter Height', required)
          .col-sm-6 
            .form-group
            label Mother Tongue
            select.form-control(name='mTongue', id='', required .dropdown-menu)
              each val in ['Sindhi' , 'Balochi' , 'Urdu' , 'Punjabi' , 'Phusto' , 'Other']
                option=val     
          .col-sm-6
            .form-group
            label Marks of Identification 
            input.form-control(type='text', name='moi', placeholder='Enter Marks of Identification ', required)      
          .col-sm-6
            .form-group
            label Disability if any
            input.form-control(type='text', name='disability', placeholder='Disability if any', required) 
          .col-sm-6
            .form-group
            label Religion
            input.form-control(type='text', name='religion', placeholder='Religion', required)      
          .col-sm-6
            .form-group
            label Mental Helth
            input.form-control(type='text', name='mHealth', placeholder='Enter Mental Helth', required) 
          //- .col-sm-6
          //-   .form-group
          //-   label Missing Person Image
          //-   input.form-control(name='profileimage', type='file')
    hr  
    h2 Information Details
    .row
          .col-sm-6
            .form-group
            label Name
            input.form-control(type='text', name='name', placeholder='Enter Name', required)      
          .col-sm-6
            .form-group
            label Contact No
            input.form-control(type='text', name='contact', placeholder='Enter Contact NO', required)  
          .col-sm-6
            .form-group
            label Address Line 1
            input.form-control(type='text', name='add3', placeholder='Enter Address Line 1', required)      
          .col-sm-6
            .form-group
            label Address Line 2
            input.form-control(type='text', name='add4', placeholder='Enter Address Line 2', required) 
          .col-sm-6
            .form-group
            label City
            input.form-control(type='text', name='city1', placeholder='Enter City', required)
          .col-sm-6
            .form-group
            label State/Province/Region
            select.form-control(name='state', id='' .dropdown-menu , required)
              each val in ['Sindh' , 'Punjab' , 'KPK' ,'Balochistan', 'Gilgit-Baltistan' , 'AJK', 'Islamabad']
                option=val  
                
          .col-sm-6
            .form-group
            label Postal / Zip Code
            input.form-control(type='text', name='postal', placeholder='Enter Postal / Zip Code', required) 
          .col-sm-6
            .form-group
            label Country
            input.form-control(type='text', name='country1', placeholder='Country', required)
          .col-sm-6
            .form-group
            label Relation to the child
            input.form-control(type='text', name='RTC', placeholder='Relation to the child', required) 
          .col-sm-6
            .form-group
            label Email
            input.form-control(type='email', name='email', id="exampleInputEmail1" , placeholder='Email', required) 
           .col-sm-6
            .form-group
            input.btn.btn-primary(type='submit', name='submit',value='Submit')`

[When i posted data ][1]

When i post the data my data is not posting in database and show error but if i find the data show on my page its shows data

  [1]: https://i.sstatic.net/pnGvC.png

Upvotes: 0

Views: 39

Answers (1)

Mattia Rasulo
Mattia Rasulo

Reputation: 1451

You are not properly handling the form with multer.

You need to add the attribute multipart/form-data to the form and ensure you add a body-parser in on your app instantiation:

app.use(express.urlencoded());

Upvotes: 0

Related Questions