如何操作请求中的 res.locals 对象?

How manipulate res.locals object in request?

我在我的项目中使用 node-restful,我想用 moment 替换我的日期属性。

当我像下面这样尝试时;

var QuestionResource = app.user = restful.model('Question', questionSchema)
        .methods(['get', 'post', 'put', 'delete']);

    QuestionResource
        .after('get', function(req, res, next) {
            res.locals.bundle.forEach(function(question) {
               res.locals.bundle[res.locals.bundle.indexOf(question)].created_at = moment(question.created_at).fromNow()
            })
            next()
        })

响应数据未更改。为什么?怎么做到的?

提前致谢。

我刚刚在 github.

上帮助 node-restful 社区解决了我的问题

仔细观察,似乎是因为返回的模型不是普通对象,而是 mongoose 对象的实例。

在post处理中,手动调用toObject()

res.locals.bundle.forEach(function(question, idx, questions) {
  questions[idx] = question.toObject();
  questions[idx].created_at = moment(question.created_at).fromNow()
})