2014-10-01 91 views
0

當我使用GET請求時,我試圖讓我的數據以mm/dd/yyyy格式的blogpost.date時間戳顯示,並顯示創建時的日期。我遇到的問題是我的數據出現在當前日期&時間和全時間戳格式中。我相信我的貓鼬模型是正確設置的,但它可能會成爲我在routes.js中請求數據的問題。使用創建的時間戳而不是當前日期MongooseJS

1)我應該從我的模式中刪除Date.now來修復當前的時間戳問題嗎? 2)日期格式會發生在我的模型中,還是最有可能的格式在routes.js文件中?

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 

回答

0

設置在模式中的日期的最簡單方法就是讓架構由默認選項做到這一點。

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

這就夠了。您不必在創建博客條目時進行設置。

所以刪除此行:

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

如果你想添加自己的時間戳(什麼,我不會推薦),則只需設置你的數據類型爲字符串/號碼,並移除默認選項:

date: { type: String } 

我會格式化,當你得到的日期您的博文(S)

檢查了這一點Timestamp to human readable format

你使用AngularJs?如果是這樣,請檢查AngularJs date filter

+0

嘿@BastianW謝謝你的回答。所以,你說刪除blogpost.date = req.body.date將解決問題,我收到每個帖子的完全相同的日期,當我用'/'路線獲取每個帖子時? – cphill 2014-10-04 15:04:39

+0

是的。當您插入博客帖時,'default:Date.now'會自動添加'日期'字段。您將在GET調用中收到創建時間。 – BastianW 2014-10-05 17:02:10

相關問題