{"id":929,"date":"2022-08-30T15:09:32","date_gmt":"2022-08-30T15:09:32","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/displaying-dates-from-mongodb-with-express-js-closed-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:09:32","modified_gmt":"2022-08-30T15:09:32","slug":"displaying-dates-from-mongodb-with-express-js-closed-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/displaying-dates-from-mongodb-with-express-js-closed-collection-of-common-programming-errors\/","title":{"rendered":"Displaying Dates from MongoDB with Express.js [closed]-Collection of common programming errors"},"content":{"rendered":"<p>I saved few datasets to MongoDB using mongoose.js. But I got a problem with displaying them on an html site using express.js and ejs.<\/p>\n<p>Here is my scenario:<\/p>\n<p><strong>Model<\/strong><\/p>\n<pre><code>var mongoose = require('mongoose');\n\nvar Schema = mongoose.Schema;\nvar ObjectId = Schema.ObjectId;\n\nvar ItemSchema = new Schema({\n    _id: ObjectId,\n    creationTime: Date,\n    modificationTime: Date,\n    title: String\n});\nvar Item = mongoose.model('item', ItemSchema);\nmodule.exports.Item = Item;\n<\/code><\/pre>\n<p><strong>Route:<\/strong><\/p>\n<pre><code>app.get('\/item\/:id', function(req, res) {\n\n    Item.findById(req.params.id, function(err, doc){\n\n        console.log(doc); \/\/This check displays everything correctly on console\n\n        res.render('item.html.ejs', {\n            item : doc\n        });\n    });\n});\n<\/code><\/pre>\n<p><strong>View:<\/strong><\/p>\n<h1><code>:<\/code><\/h1>\n<pre>\n<br \/>Creation: \n<br \/>Modification: \n<\/pre>\n<p><code>The result of this setting is that title is being displayed correctly while both dates are <code>undefined<\/code>.<\/code><\/p>\n<p>I assume it has something to do with MongoDB&#8217;s ISODate format. But I can&#8217;t find a solution on how to convert it for displaying in html views.<\/p>\n<p>I appreciate your help. Cheers<\/p>\n<ol>\n<li>\n<p>If you just copied and pasted the code the solution is very very simple.<\/p>\n<p>In your model you defined creation<code>Time<\/code> and modification<code>Time<\/code> in your template you try to access item.creation<code>Date<\/code> and item.modification<code>Date<\/code>.<\/p>\n<p>After that change you should be able to see something other then undefined, but you probably still need to convert this into a proper date format.<\/p>\n<\/li>\n<li>\n<p>I have the following helpers using moment.js in my projects.<\/p>\n<pre><code>date: function(date){\n  moment(date).format('YYYY-MM-DD hh:mm:ss');\n},\nfromNow: function(date){\n  moment(date).fromNow()\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<p>I add an ejs filter using the example above, but I had to change it a little bit to get it to work:<\/p>\n<p>Create the &#8216;created_at&#8217; attribute on the mongoose schema and automatically create it on save():<\/p>\n<pre><code>var ItemSchema = new Schema({\n    name    : { type: String, required: true, trim: true }\n  , created_at    : { type: Date }\n});\n\n\nItemSchema.pre('save', function(next){\n  if ( !this.created_at ) {\n    this.created_at = new Date;\n  }\n  next();\n});\n<\/code><\/pre>\n<p>Install moment (and write to package.json)<\/p>\n<pre><code>npm install moment --save\n<\/code><\/pre>\n<p>Add the filter in app.js or wherever else you define filters for ejs:<\/p>\n<pre><code>var moment = require('moment');\n\nejs.filters.fromNow = function(date){\n  return moment(date).fromNow()\n}\n<\/code><\/pre>\n<p>Use the filter in page.ejs<\/p>\n<pre><code>\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:59:29. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I saved few datasets to MongoDB using mongoose.js. But I got a problem with displaying them on an html site using express.js and ejs. Here is my scenario: Model var mongoose = require(&#8216;mongoose&#8217;); var Schema = mongoose.Schema; var ObjectId = Schema.ObjectId; var ItemSchema = new Schema({ _id: ObjectId, creationTime: Date, modificationTime: Date, title: String }); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-929","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/929","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=929"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/929\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=929"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=929"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=929"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}