【问题标题】:MongoDB: slug is undefinedMongoDB:蛞蝓未定义
【发布时间】:2022-11-09 17:08:31
【问题描述】:

每个模式都有自己的slugmongoose-slug-generatorschemaMain 生成 slug,但对于 schema1,slug 为 undefined

无法理解为什么它未定义。

import mongoose from 'mongoose'
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,
        slug: ["name"],
        slug_padding_size: 4
    },
    description: {
        type: String,
        min: 1,
        max: 200,
        required: true,
        trim: true
    },
    video: {},
    music: []
}, { timestamps: true })


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)

【问题讨论】:

    标签: node.js mongodb express mongoose


    【解决方案1】:

    而不是mongoose-slug-generator 使用mongoose-slug-updater

    你所拥有的是嵌套模式,mongoose-slug-updater 也适用于此。

    单独声明的嵌套模式示例

    const SubChildSchema = new mongoose.Schema({
      title: { type: String },
      slug: { type: String, slug: 'title' },
      absoluteRootSlug: { type: String, slug: '/title' },
      absoluteChildSlug: { type: String, slug: '/child.title' },
      relativeParentSlug: { type: String, slug: ':title' },// child's title
      relativeGrandParentSlug: { type: String, slug: '::title' },//parent's title
    });
    
    const ChildSchema = new mongoose.Schema({
      title: { type: String },
      subChild: SubChildSchema,
      subChildren: [SubChildSchema],
      slug: { type: String, slug: 'title' },
      subChildSlug: { type: String, slug: 'subChild.title' },
      absoluteSlug: { type: String, slug: '/child.title' },
      absoluteRootSlug: { type: String, slug: '/title' },
      relativeParentSlug: { type: String, slug: ':title' },//Parent
      subChildrenSlug2: { type: String, slug: 'subChildren.2.title' },
      subChildrenSlug3: { type: String, slug: 'subChildren.3.title' },
    });
    
    const ParentSchema = new mongoose.Schema({
      title: { type: String },
      child: ChildSchema,
      children: [ChildSchema],
      slug: { type: String, slug: 'title' },
      absoluteSlug: { type: String, slug: '/title' },
      childSlug: { type: String, slug: 'child.title' },
      absoluteChildSlug: { type: String, slug: '/child.title' },
      subChildSlug: { type: String, slug: 'child.subChild.title' },
      childrenSlug0: { type: String, slug: 'children.0.title' },
      childrenSlug4: { type: String, slug: 'children.4.title' },
      subChildrenSlug3: { type: String, slug: 'children.7.subChildren.3.title' },
      subChildrenSlug7: { type: String, slug: 'children.3.subChildren.7.title' },
    });
    

    你可以阅读更多关于它的信息here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-27
      • 2013-03-24
      • 1970-01-01
      • 1970-01-01
      • 2012-05-17
      • 2015-01-07
      • 2011-01-30
      • 2014-07-30
      相关资源
      最近更新 更多