【发布时间】:2023-03-21 09:43:01
【问题描述】:
我希望用户对象保持区分大小写以用于显示目的,但为了唯一性目的而将其小写。我的第一个想法是在架构中添加 usernameDisplay 属性并尝试预保存挂钩:
var userSchema = new Schema({
username: {
type: String,
required: true,
unique: true,
lowercase: true
},
usernameDisplay: String,
password: {
type: String,
required: true
}
});
userSchema.pre("save", function (next) {
this.usernameDisplay = this.username;
next();
});
但这似乎不起作用。 username 和 usernameDisplay 属性都保存为小写用户名。
我认为以下方法可行:删除架构中的 lowercase 验证并执行此操作:
userSchema.pre("save", function (next) {
this.userDisplayName = this.username;
this.username = this.username.toLowerCase();
next();
});
但现在我很好奇 Mongoose 如何对传入数据进行小写验证/更改。
如果我的问题不清楚,请告诉我,我可以尝试更新它以澄清问题。
【问题讨论】:
标签: javascript node.js mongoose