Mongoose Date Schema

守給你的承諾、 提交于 2019-12-11 16:53:34

问题


I am using MongoDB, Mongoose, Express and Express Handlebars.

I have an <input type="date"> in my form, that posts to database, and my mongoose schema is defined like this:

var recordsSchema = new mongoose.Schema ({
   scheduled: Date,
});

The form inserts the time, but the output looks something like this:

Fri Oct 26 2018 00:00:00 GMT+0000 (UTC)

I want to format it so it appears as:

Fri Oct 26 2018

And not have anything else.

I tried using toString() method in the Handlebars template, but it doesn't work and server throws an error.

This is how it looks on the Express-Handlebars side:

{{ this.scheduled }} {{ this.scheduled.toString() }}

Can someone please let me know how to solve this?

Thanks.


回答1:


Try to add a custom getter in the schema to get the formatted date string as expected.

var recordsSchema = new Schema ({
  scheduled: Date,
  get: value => value.toDateString()
});


来源:https://stackoverflow.com/questions/52867578/mongoose-date-schema

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!