首页 > 解决方案 > 在它自己的模式中使用模式属性?猫鼬节点js

问题描述

有没有办法可以在它自己的模型中使用模式属性

    var mongoose = require("mongoose");

//====================================================
// Schema 
//====================================================

var bookSchema = new mongoose.Schema({
    name: String,
    image: String,
    summary: String,
    author: String,
    genre: String,
    publisher: String,
    available: Number,
    submitted: {
        id: {
            type: mongoose.Schema.Types.ObjectId,
            ref: "User" //model name
        },
        username: String
    },
    reviews: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: "Review"
        }
        ],
    reserves: {
        type: [{
            type: mongoose.Schema.Types.ObjectId,
            ref: "Reserve" 
        }],
        validate: [arrayLimit, '{PATH} exceeds the limit of 5']
    }
});


function arrayLimit(val) {
    return val.length <= 5;
}

module.exports = mongoose.model("Book", bookSchema);

在函数arraylimit中。我正在尝试用 bookSchema 中的“可用”属性替换“5”。

标签: node.jsmongoose

解决方案


推荐阅读