2017-05-19 24 views
0

我試圖將當前日期作爲對象屬性,並將其轉換爲Json作爲節點後端。我也不知道,如果在節點中的日期屬性將捕獲例如從前端如何在角度對象中使用日期管道並將其格式設置爲節點的JSON?

在角2/4我有

const newCustomer = { 
    firstName: this.firstName; 
    lastName: this.lastName; 
    date: number = Date.now(); 
} 

我在節點router.post

router.post('/customers', (req,res,next) =>{ 

let newCustomer = new Customer({ 

    firstName: req.body.firstName, 
    lastName: req.body.lastName, 
    date: Date.now() 


}); 

newCustomer.save(function(err, Customer){ 

    if (err){ 

     res.json({msg: 'Failed to add contact'}); 

    }else{ 

     res.json({msg: 'Customer added to waitlist'}); 

    } 
}); 

我的模型/架構爲客戶

const customerSchema = new Schema({ 

date: new Date(); 

firstName: { 
    type: String, 
    //required means there has to be a name 
    required: true 
}, 

lastName: { 
    type: String, 
    required: true 
} 


}); 

回答

0

由於要傳遞的日期對象作爲JSON的後端這應該被序列化,但是您可以在節點應用程序中創建一個新的Date對象來解析序列化的日期對象。 Ref toJSON

var backendDate = new Date(jsonSerializedDate); 
相關問題