【发布时间】:2023-03-17 03:27:01
【问题描述】:
因为我无法编辑非精益猫鼬结果的属性,所以我使用了result.toObject() 语句,但这也意味着我无法使用架构上定义的方法。
例子
// Defining the schema and document methods
const User = new Schema({
email: {type: String, required: true, unique: true},
firstname: {type: String, required: true},
registration_date: {type: Date, default: Date.now, required: true},
insert: {type: String},
lastname: {type: String, required: true}
});
User.methods.whatIsYourFirstName = function () {
return `Hello, my firstname is:${this.firstname}`;
};
找到后:
user = user.toObject();
user.registration_date = moment(user.registration_date);
user.whatIsYourFirstName();
// result in "user.whatIsYourFirstName is not a function"
这可以解决吗?
【问题讨论】:
-
原始代码有一个名为
registration_date的属性,该值被momentjs 值覆盖。在之前的快速路由中,我需要那些用于中间件验证的方法,之后它被称为 toObject,但是一些特定的路由需要那些在调用.toObject()时被丢弃的额外方法。