1
我在看猫鼬的API文档,发现get选项。但它似乎不适合我。猫鼬得不到工作(SchemaType#get(fn))
这与获取选项的架构:
var PostSchema = new Schema({
content: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now,
get: function (val) {
return (val.getMonth() + 1) + "/" + val.getDate() + "/" + val.getFullYear() + " " + (val.getHours() + 1) + ":" + (val.getMinutes() + 1) + ":" + (val.getSeconds() + 1);
}
}
})
这是我取的所有文件:
var Post = App.model('post')
exports.fetchAll = function (req, res, next) {
Post.find({}).sort({date: 'desc'}).exec(function (err, posts) {
if (err) { return next(err) }
res.json(posts)
})
}
但结果还是一样。在客户端,我收到{{post.date}}非格式化字符串:
2015-10-18T07:56:24.606Z
我想不通为什么格式化日期字符串没有得到恢复。
什么是'应用',特别是'App.model('post')'?那是'mongoose'的例子吗? – Svenskunganka
只是一个包含一些助手的全局变量。它返回猫鼬模型:global.App = {...模型:函数(名称){0}返回require('./../ app/models /'+ name) },...} – user2861867
您可以添加'console.log(val);''在返回之前(val.getMonth..'来看看val是什么?我只是猜测,但它可能是字符串,所以你应该使用'val = new Date(val); ' – Molda