Node.js 转换mongoose架构内的属性

Node.js 转换mongoose架构内的属性,node.js,typescript,express,mongoose,Node.js,Typescript,Express,Mongoose,我正在开发一组模型,我需要将传入的名称转换为规范链接。这是我的模型 import mongo from 'mongoose' const schema = new mongo.Schema({ name: { type: String, required: [true, 'El nombre de la marca es necesaria' ], unique: [true, 'Ya existe esta marca'],

我正在开发一组模型,我需要将传入的名称转换为规范链接。这是我的模型

import mongo from 'mongoose'

const schema = new mongo.Schema({
    name: { 
        type: String, 
        required:  [true, 'El nombre de la marca es necesaria' ],
        unique:    [true, 'Ya existe esta marca'],
        minlength: [3, 'Al menos 3 caracteres'],
        maxlength: [100, 'Máximo 100 caracteres'],
    },
    link: { 
        type: String,
        default: function() {
            return this.name
            .normalize("NFD")
            .replace(/[\u0300-\u036f]/g, "")
            .replace(/\s+/g, '-')
            .toLowerCase()
        }
    },
    active: { type: Boolean, default: true }

}, { collection: 'moto-brands' })

export const MotoBrandModel = mongo.model( 'MotoBrand', schema )
此时,Typescript向我显示一个错误,服务器也会显示。 该错误标记在
此。名称

类型“SchemaTypeOpts |架构”上不存在属性“name”| 模式类型'

类型“SchemaTypeOpts”上不存在属性“name”

这可能是模拟

Incoming request
{ name: '   El Pingüino maléfico ' }
猫鼬模型必须转换为
el pinguino malefico
,并设置为
link
索引作为默认值。
该函数工作正常,但我做错了什么?

文档中说:“除非它在使用setDefaultsOnInsert的查询上运行,否则默认函数的this指的是文档”。因此,请确保在调用模型的查询中没有使用setDefaultsOnInsert。谢谢毛罗。我的问题不同,我无法将数据插入模型,因为
此.name
不起作用。