display a date on an edit from from mongo using ejs

柔情痞子 提交于 2019-12-25 04:27:48

问题


There is an edit page where a user can, obviously, edit aspects of an event. Everything displays fine except the Date shows empty.

I am using the type="date" in my html which may be a cause, how can I get around this so that I can show the date, because when it saves it saves as null after editing the event

View:

 <div class="form-group">
    <input class="form-control" type="date" name="editEvent[startDate]" placeholder="Date" value="<%= event.startDate %>">
 </div>

Route:

router.get("/event/:id/edit", isLoggedIn, function(req,res){
Event.findById(req.params.id, function (err, foundEvent) {
    if(err){
        console.log(err);
        console.log("Cannot find the event...maybe the database isnt running?")
    } else {
        res.render("eventEdit", {event: foundEvent});

Everything works fine but the date


回答1:


Can you add some more information to the question? at the moment it looks quite unclear. like how is your server set up.




回答2:


It turns out its an HTML thing, if you want to display a date you have to have it formattted as YYYY-MM-DD then it shows fine, otherwise it doenst recognize the format.

This comes into account if you use the type="date" attribute in your input

I had to edit the event date coming from mongo using momentjs and here is the line I used.

var newDate = moment(foundEvent.startDate).utc().format("YYYY-MM-DD") res.render("eventEdit", {event: foundEvent, newDate:newDate});

The newDate is the variable being passed to the template, then I just have:

 <input class="form-control" type="date" name="editEvent[startDate]" placeholder="Date" value="<%= newDate %>">

This works.



来源:https://stackoverflow.com/questions/40942473/display-a-date-on-an-edit-from-from-mongo-using-ejs

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