【问题标题】:display a date on an edit from from mongo using ejs使用 ejs 在来自 mongo 的编辑中显示日期
【发布时间】:2017-04-17 23:17:00
【问题描述】:

有一个编辑页面,用户显然可以在其中编辑事件的各个方面。一切都显示正常,除了日期显示为空。

我在我的 html 中使用了type="date",这可能是一个原因,我该如何解决这个问题以便我可以显示日期,因为当它保存时它会在编辑事件后保存为 null

查看:

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

路线:

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

一切正常,但日期

【问题讨论】:

    标签: mongodb date ejs


    【解决方案1】:

    您能否在问题中添加更多信息?目前看起来很不清楚。比如你的服务器是如何设置的。

    【讨论】:

    • 我在问题中添加了路线,您还需要什么?
    • 我会说设置您的 mongo 模式以获取 startDate: Date 然后检查以确保数据库中的 startDate 不为空。此外,为了进一步诊断问题,请尝试在路线中拆分 foundEvent。就像只添加 eDate:foundEvent.startDate。 .....你最初能看到 startDate 的内容吗? .. 我想看看你调用的保存文件的函数
    【解决方案2】:

    原来它是一个 HTML 的东西,如果你想显示一个日期,你必须把它格式化为 YYYY-MM-DD 然后它显示得很好,否则它不识别格式。

    如果您在输入中使用type="date" 属性,则会考虑到这一点

    我必须使用 momentjs 编辑来自 mongo 的事件日期,这是我使用的行。

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

    newDate 是传递给模板的变量,那么我只有:

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

    这行得通。

    【讨论】:

      猜你喜欢
      • 2020-06-24
      • 1970-01-01
      • 1970-01-01
      • 2018-03-15
      • 2016-03-23
      • 1970-01-01
      • 2017-04-07
      • 2012-06-08
      • 2019-06-01
      相关资源
      最近更新 更多