モデルを表示する前に、モデルの日付型プロパティをフォーマットしようとしています。これは私が使用しているコードです:
// MODEL
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var ArticleSchema = new Schema({
title: String,
content: String,
author: { type: String, default: 'Vlad'},
postDate: { type: Date, default: Date.now }
});
ArticleSchema.methods.formatTitle = function() {
var link = this.title.replace(/\s/g, '-');
return '/article/' + link;
};
ArticleSchema.methods.snapshot = function() {
var snapshot = this.content.substring(0, 500);
return snapshot;
};
ArticleSchema.methods.dateString = function() {
var date = new Date(this.postDate);
return date.toDateString();
};
module.exports = mongoose.model('Article', ArticleSchema);
そしてクライアント側では、次を使用してフォーマットされた日付を表示しようとします:
{{ article.dateString }}
それでも、この要素を含むビューをロードするたびに、500 エラーが発生します。
Cannot call method 'toDateString' of undefined
EDIT1: ビューに {{ article.snapshot }} を埋め込むことに問題はありませんが、Date オブジェクトに関してはエラーが発生します
EDIT2: console.log(article.dateString()) を使用して dateString メソッドをログに記録すると、次のようになります。
Wed Sep 18 2013
EDIT3: これは、dankohn が提供するコードを使用したときに得られるものです。それは私だけですか、それともメソッドを 2 回続けて実行しているだけですか?
this.postdate: Wed Sep 18 2013 23:27:02 GMT+0300 (EEST)
parsed: 1379536022000
date: Wed Sep 18 2013 23:27:02 GMT+0300 (EEST)
toString: Wed Sep 18 2013
Wed Sep 18 2013
this.postdate: undefined
parsed: NaN
date: Invalid Date
toString: Invalid Date