小编Syn*_*vea的帖子

为什么我的Mongoose 3.8.7模式getter和setter被忽略了?

在使用Node.js,Mongoose和MongoDB时,我发现当我执行findOne查询时,我的Mongoose模式getter和setter不会触发.

我发现一个旧线程表明版本2.x中存在getter和setter的问题,但它表示它已经解决,我使用的是最新版本的Mongoose(3.8.7).

这是我的架构的一部分

function testGetter(value) {
        return value + " test";
}

/**
* Schema
*/

var schema = new Schema({
        username: { type: String, required: true, unique: true, get: testGetter }
});

// I have also tried this.

schema.path('username').get(function (value, schemaType) {
        return value + " test";
});
Run Code Online (Sandbox Code Playgroud)

这是我执行查询的方式

Model
.findOne(conditions, fields, options)
.populate(population)
.exec(function (error, doc) {
        callback(doc, error);
});
Run Code Online (Sandbox Code Playgroud)

它使用缺少"测试"后修复的用户名值进行响应.我在这里做错了吗?任何帮助将不胜感激!

附加信息

这是找到一个的结果:

{
    "username": "Radius"
}
Run Code Online (Sandbox Code Playgroud)

在应用上述两种方法之一后,这是schema.paths.username.getters的值:

[ [Function: testGetter] ]
Run Code Online (Sandbox Code Playgroud)

getter setter schema mongoose node.js

11
推荐指数
2
解决办法
7145
查看次数

标签 统计

getter ×1

mongoose ×1

node.js ×1

schema ×1

setter ×1