You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

mongoose-slug-generator生成嵌套schema1的slug为undefined问题求助

问题:嵌套Schema的slug无法生成

使用mongoose-slug-generator插件时,主Schema(schemaMain)的slug能正常生成,但嵌套在其中的子Schema(schema1)的slug始终为undefined。

原因

mongoose-slug-generator的全局插件仅对被mongoose.model()实例化为模型的顶级Schema生效。嵌套的子Schema不会被插件自动扫描处理,因为插件的slug生成钩子只绑定在顶级模型的Schema上,子文档的Schema不会触发这些钩子逻辑。

解决方法

方法1:给子Schema单独注册插件

在定义子Schema后,单独为其注册mongoose-slug-generator插件,让插件处理子Schema的slug字段:

import mongoose from 'mongoose'
const { Schema } = mongoose
var slug = require("mongoose-slug-generator");
mongoose.plugin(slug); // 全局注册插件(处理主Schema)

const schema1 = new Schema({
    name: {
        type: String,
        required: true,
        trim: true,
        min: 1,
        max: 40,
    },
    slug: {
        type: String,
        lowercase: true,
        slug: ["name"],
        slug_padding_size: 4
    },
    description: {
        type: String,
        min: 1,
        max: 200,
        required: true,
        trim: true
    },
    video: {},
    music: []
}, { timestamps: true })

schema1.plugin(slug); // 单独给子Schema注册插件

const schemaMain = new Schema({
    name: {
        type: String,
        required: true,
        trim: true,
        min: 1,
        max: 40,
    },
    slug: {
        type: String,
        lowercase: true,
        slug: ["name"],
        unique: true,
        slug_padding_size: 4
    },
    description: {
        type: String,
        min: 1,
        max: 200,
        required: true,
        trim: true
    },
    schemas: [schema1],
}, { timestamps: true })

export default mongoose.model('SomeModel', schemaMain)

方法2:手动编写预保存钩子生成slug

如果不想依赖插件的自动逻辑,可以使用slugify库手动编写子Schema的预保存钩子,生成slug:

  1. 先安装slugify:
npm install slugify
  1. 修改代码:
import mongoose from 'mongoose'
import slugify from 'slugify'
const { Schema } = mongoose
var slug = require("mongoose-slug-generator");
mongoose.plugin(slug);

const schema1 = new Schema({
    name: {
        type: String,
        required: true,
        trim: true,
        min: 1,
        max: 40,
    },
    slug: {
        type: String,
        lowercase: true
    },
    description: {
        type: String,
        min: 1,
        max: 200,
        required: true,
        trim: true
    },
    video: {},
    music: []
}, { timestamps: true })

// 给子Schema添加预保存钩子生成slug
schema1.pre('save', function(next) {
    // 当name字段被修改且slug不存在时生成
    if (this.isModified('name') && !this.slug) {
        // 生成带随机后缀的slug,模拟插件的slug_padding_size功能
        const baseSlug = slugify(this.name, { lower: true, strict: true })
        const suffix = Math.random().toString(36).substring(2, 6)
        this.slug = `${baseSlug}-${suffix}`
    }
    next()
})

const schemaMain = new Schema({
    name: {
        type: String,
        required: true,
        trim: true,
        min: 1,
        max: 40,
    },
    slug: {
        type: String,
        lowercase: true,
        slug: ["name"],
        unique: true,
        slug_padding_size: 4
    },
    description: {
        type: String,
        min: 1,
        max: 200,
        required: true,
        trim: true
    },
    schemas: [schema1],
}, { timestamps: true })

export default mongoose.model('SomeModel', schemaMain)

内容的提问来源于stack exchange,提问作者George

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 19:35:37