2014-12-02 11 views
5

Dostaję błąd sprawdzania poprawności z następującym komunikatem na zapisywanie do bazy danych, mimo że podałeś wszystkie pola,Uzyskiwanie błąd sprawdzania poprawności w mangusta chociaż pliki są przewidziane

{ [ValidationError: Validation failed] 
    message: 'Validation failed', 
    name: 'ValidationError', 
    errors: 
    { Name: 
     { [ValidatorError: Path `Name` is required.] 
     message: 'Path `Name` is required.', 
     name: 'ValidatorError', 
     path: 'Name', 
     type: 'required', 
     value: undefined } } } 

ten sposób obiekt, który próbuję zapisać wygląda jak

{ Name: 'Nobody Singh', 
    phone: '+919177121364', 
    address: 'flat no 306 koratala apartments\nstreet no 3 ,Himayatnagar, Near Siraj plaza', 
    start: '2014-12-03T13:00:00.000Z', 
    end: '2014-12-03T15:00:00.000Z', 
    details: 'flat no 306 koratala apartments\nstreet no 3 ,Himayatnagar, Near Siraj plaza' } 

Oto schemat

// load the things we need 
var mongoose = require('mongoose'); 

// define the schema for our user model 
var appointmentSchema = mongoose.Schema({ 
     email: { type: String, default: '[email protected]' },   
     name: { type: String, required:true }, 
     phone: { type:Number }, 
     address:{ type: String }, 
     details:{ type: String }, 
     title:{ type: String, default: "Slot Taken"}, 
     start: { type:Date}, 
     end: { type:Date}, 
     requestedDate: { type:Date, default: Date.now } 

}); 



// create the model for users and expose it to our app 
module.exports = mongoose.model('Appointment', appointmentSchema); 

H ere jest plik trasa

app.post('/saveappointment', function(req, res) { 

var appointment = new Appointment(); 

    var appointMent = { 
         //need to add an email here 
         name: req.body.name, 
         phone: req.body.phone, 
         address: req.body.address, 
         start: req.body.appointment.start, 
         end:req.body.appointment.end, 
         details:req.body.details, 
         address:req.body.address 
        }; 

    console.log(appointMent); 

    appointment.save(appointMent, 
    function(err,resp) { 
     if(err) { 
      console.log(err); 
      res.send({ 
       message :'something went wrong' 
      }); 
     } else { 
      res.send({ 
       message:'the appointment has bees saved' 
      }); 
     }   

    }); 
}) 

Odpowiedz

9

Spróbuj poniżej kod i daj mi znać, jeśli ten sam błąd pojawi

app.post('/saveappointment', function(req, res) { 

    var appointment = new Appointment({ 
    //need to add an email here 
    name: req.body.name, 
    phone: req.body.phone, 
    address: req.body.address, 
    start: req.body.appointment.start, 
    end: req.body.appointment.end, 
    details: req.body.details, 
    address: req.body.address 
    }); 


    appointment.save(function(err, resp) { 
    if (err) { 
     console.log(err); 
     res.send({ 
     message: 'something went wrong' 
     }); 
    } else { 
     res.send({ 
     message: 'the appointment has been saved' 
     }); 
    } 

    }); 
}); 
+1

To zadziałało jak urok: D możesz wyjaśnić, co zrobiłem źle? – Bazinga777

+2

Twój błąd polegał na tym, że wstawiłeś obiekt do zapisu manga, poprawny sposób to wywołać funkcję zapisywania na obiekcie mongoose.model i podać tylko wywołanie zwrotne jako jego parametr, a nie dodatkowy obiekt . Zobacz dokumentację: http://mongoosejs.com/docs/models. html – kaxi1993

0

Musisz zainstalować (npm zainstalować ciało-parser --save), i użyj go na swoim ekspresowym serwerze jak poniżej (app.js). Mam nadzieję, że ta odpowiedź pomoże komuś :)

var express = require('express') 
var bodyParser = require('body-parser') 
var app = express() 

app.use(bodyParser.json()) 

app.use('/api',api) 
module.exports = app 
Powiązane problemy