2014-12-02 57 views
5

我收到以下消息驗證錯誤上保存到即使我已經提供了所有領域的數據庫中貓鼬驗證錯誤,獲取即使提供文件

{ [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 } } } 

這是怎樣的對象,我想保存的樣子

{ 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' } 

的這裏的架構

// 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是路由文件

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' 
      }); 
     }   

    }); 
}) 

回答

9

下面試試這個代碼,讓我知道,如果同樣的錯誤會出現

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

這就像一個魅力:D你能解釋它是什麼,我做錯了嗎? – Bazinga777 2014-12-02 08:43:30

+2

你的錯誤是你將對象插入貓鼬保存,正確的方法是調用mongoose.model對象上的保存功能,並提供只回調,因爲它的參數不附加對象 請參閱文檔:http://mongoosejs.com/docs/models。 HTML – kaxi1993 2014-12-02 09:13:26

0

您需要安裝(NPM安裝體解析器--save)並在您的快遞服務器中使用它(如下所示)(app.js)。希望這個答案可以幫助某人:)

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

app.use(bodyParser.json()) 

app.use('/api',api) 
module.exports = app 
相關問題