mongoosejs在数组字段上保存挂钩

时间:2014-06-09 12:48:33

标签: mongodb mongoose

我希望所有类别都是小写的 我试过这个:

ArticleSchema.pre('save', function(next) {

    _.map(this.categories, function(category) { 
        console.log(category.toLowerCase());
        return category.toLowerCase();  
    });    
    next();
});

但它不起作用(如果我插入即PHP,JAVA我发现PHP,JAVA)

出了什么问题?

1 个答案:

答案 0 :(得分:1)

map()没有修改原始数组,您需要将结果分配给属性:

ArticleSchema.pre('save', function(next) {

    this.categories = _.map(this.categories, function(category) { 
        console.log(category.toLowerCase());
        return category.toLowerCase();  
    });    
    next();
});
相关问题