转换猫鼬架构内的属性

时间:2019-12-02 21:02:30

标签: 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'],
        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向我显示错误,服务器也是如此。 错误标记在this.name

  

类型“ SchemaTypeOpts |属性”上不存在属性“名称”架构|   SchemaType”。

     

类型“ SchemaTypeOpts”上不存在属性“名称”

这可能是模拟

Incoming request
{ name: '   El Pingüino maléfico ' }

猫鼬模型必须转换为el-pinguino-malefico并设置为link索引作为默认值。 该功能运行正常,但是我在做什么错了?

0 个答案:

没有答案