post.time.minute属性怎么会为null
发布于 9 年前 作者 maczyt 3146 次浏览 最后一次编辑是 8 年前 来自 问答

小白在学习nswbmw大神的搭建博客教程在post文章那里,遇到如下问题,请cnode大神帮忙解决 function Post(name, title, post) { this.name = name; this.title = title; this.post = post; } module.exports = Post; Post.prototype.save = function (callback) { var date = new Date(); var time = { date: date, year: date.getFullYear(), month: date.getFullYear + “-” + (date.getMonth() + 1), day : date.getFullYear() + “-” + (date.getMonth() + 1) + “-” + date.getDate(), minute : date.getFullYear() + “-” + (date.getMonth() + 1) + “-” + date.getDate() + " " + date.getHours() + “:” + (date.getMinutes() < 10 ? ‘0’ + date.getMinutes() : date.getMinutes()) } 我在模板index.ejs上引用上面的模型post.js 出现下面的错误 Cannot read property ‘minute’ of null at __line (eval at <anonymous> (/home/zhouyt/blog/node_modules/ejs/lib/ejs.js:464:12), <anonymous>:72:32) at Array.forEach (native) at eval (eval at <anonymous> (/home/zhouyt/blog/node_modules/ejs/lib/ejs.js:464:12), <anonymous>:63:14) at returnedFn (/home/zhouyt/blog/node_modules/ejs/lib/ejs.js:493:17) at View.exports.renderFile [as engine] (/home/zhouyt/blog/node_modules/ejs/lib/ejs.js:350:31) at View.render (/home/zhouyt/blog/node_modules/express/lib/view.js:126:8) at tryRender (/home/zhouyt/blog/node_modules/express/lib/application.js:639:10) at EventEmitter.render (/home/zhouyt/blog/node_modules/express/lib/application.js:591:3) at ServerResponse.render (/home/zhouyt/blog/node_modules/express/lib/response.js:961:7) at /home/zhouyt/blog/routes/index.js:20:8

调用的代码为:

Date: <%= post.time.minute%>

可能提问题的格式有问题,不过请各位大大见谅

3 回复

漏了要存入数据库文档结构

Post.prototype.save = function(callback) {
	var date = new Date();
	//存储各种时间格式方便以后使用
	var time = {
		date : date,
		year : date.getFullYear(),	
		month : date.getFullYear()+’-‘+(date.getMonth()+1),
		day : date.getFullYear()+’-‘+(date.getMonth()+1)+’-‘+date.getDate(),
		hour : date.getFullYear()+’-‘+(date.getMonth()+1)+’-‘+date.getDate()+’-‘+date.getHours(),
		minute : date.getFullYear()+’-‘+(date.getMonth()+1)+’-‘+date.getDate()+’ '+date.getHours()+’:’+date.getMinutes()
	};
	//要存入数据库的文档
	var post = {
		name : this.name,
		time : time,
		title : this.title,
		tags : this.tags,	
		post : this.post,
		comments : []
	};
}

修改了post.js

var post = {
		name: this.name,
		time: time,
		title: this.title,
		post: this.post
	};

还是出现问题,post的time属性是一个对象,但是却没有toString() 方法,很疑惑

Cannot call method ‘toString’ of null 代码:

<%= post.time.toString() %>

time明明写着

var time = {
		date: date,
		year: date.getFullYear(),
		month: date.getFullYear() + "-" + (date.getMonth() + 1),
		day : date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate(),
		minute : date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + " " + 
		date.getHours() + ":" + (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) 
	};

怎么会是null呢?

回到顶部