0

When I use the GET request I am trying to have my data appear with my blogpost.date timestamp in mm/dd/yyyy format and show the date when it was created. The issue that I am running into is that my data is appearing with the current date & time and in the full timestamp format. I believe the my mongoose model is correctly set up, but it could be an issue with how I am requesting the data in my routes.js.

1) Should I remove Date.now from my schema to fix the current timestamp issue? 2) Would the date formatting occur in my model or most likely formatting in the routes.js file?

blogModel.js:

var mongoose    = require('mongoose');
var mongoosePaginate = require('mongoose-paginate');
var Schema      = mongoose.Schema;



var BlogPostSchema  = new Schema({
        title: String,
        author: String,
        tagline: String,
        content: String,
        category: String,
        tags: { type: String, lowercase: true },
        date: { type: Date, default: Date.now }
});

BlogPostSchema.plugin( mongoosePaginate );

var Blogpost = mongoose.model("Blogpost", BlogPostSchema);



module.exports = mongoose.model('Blogpost', BlogPostSchema);

routes.js:

var express = require('express');
var router = express.Router();
var blogDB = require('../config/blogDB.js');
var Blogpost = require('./models/blogModel.js');
var paginate = require('express-paginate');

//index 
router.use(paginate.middleware(10, 50));

    router.route('/') 

        // START POST method
        .post(function(req, res) {

            var blogpost = new Blogpost(); // create a new instance of a Blogpost model

            blogpost.title = req.body.title; // set the blog title
            blogpost.author = req.body.author; // set the author name
            blogpost.tagline = req.body.tagline; // set the tagline
            blogpost.content = req.body.content; // set the blog content
            blogpost.category = req.body.category; // set the category
            blogpost.tags = req.body.tags; // set the tags
            blogpost.date = req.body.date; // set the date of the post
                //Save Blog Post
                blogpost.save(function(err) {
                    if (err)
                        res.send(err);

                    res.json({ message: 'Blog created.' });
                });

        }) // END POST method


        // START GET method
        .get(function(req, res, next) {

            Blogpost.paginate({}, req.query.page, req.query.limit, function(err, pageCount, blogpost, itemCount) {




                if (err) return next(err)

                        if (err)
                            res.send(err);


                        blogpost.title = req.body.title; // get the blog title
                        blogpost.author = req.body.author; // get the author name
                        blogpost.tagline = req.body.tagline; // get tagline
                        blogpost.content = req.body.content; // get the blog content
                        blogpost.category = req.body.category; // get the category
                        blogpost.tags = req.body.tags; // get the tags
                        blogpost.date = req.body.date; // get the date of the post


                        res.format({
                            html: function() {
                                res.render('pages/index', {
                                    blogpost: blogpost,
                                    pageCount: pageCount,
                                    itemCount: itemCount
                                })
                            },
                            json: function() {

                                res.json({
                                    object: 'blogpost',
                                    has_more: paginate.hasNextPages(req)(pageCount),
                                    data: blogpost
                                })
                            }
                        }); // END res.format(html, json)
            }); // END Blogpost.paginate
        }); // END GET method
cphill
  • 5,596
  • 16
  • 89
  • 182

1 Answers1

0

The easiest way to set the date in your Schema is to let the Schema do it by the default option.

date: { type: Date, default: Date.now }

That is enough. You haven't to set it on your blog entry creation.

So remove this line:

 blogpost.date = req.body.date; // set the date of the post

If you want to add your own timestamp (what I wouldn't recommend), then just set your data type to String/Number and remove the default option:

 date: { type: String }

I would format the date when you get your blogpost(s)

Check this out Timestamp to human readable format

Do you use AngularJs? If so, check the AngularJs date filter out.

Community
  • 1
  • 1
BastianW
  • 270
  • 2
  • 17
  • Hey @BastianW thanks for the answer. So you're saying that removing the blogpost.date = req.body.date will solve the issue where I am receiving the exact same date for every post when I GET every post with my '/' route? – cphill Oct 04 '14 at 15:04
  • Yes. `default: Date.now` will automatically add the 'date' field when you insert a blogpost. You will receive the creation time then on your GET calls. – BastianW Oct 05 '14 at 17:02