Mongoose Schema继承:entity.entitySchema.extend不是函数

时间:2017-03-30 18:46:54

标签: javascript node.js mongodb mongoose

我已经定义了一个基本模式,如下所示,并使用mongoose-schema-extend

testEntitySchema.js

var entitySchema = {
    entityId: {
        type: String
    },
    name: {
        type: String
    },
    description: {
        type: String
    },
    type: {
        type: String
    },
    subcategory: {
        type: String
    }
};
module.exports.entitySchema = entitySchema

我正在尝试扩展上述架构并创建如下模型,

module.exports = function (Service) {
    var extend = require('mongoose-schema-extend');
    entity = require('./testEntitySchema');
    var model = null;
    var modelName = '_entity';
    console.log("entity schema is" + entity);
    try {
        model = Service.getModel(modelName);
    } catch (error) {
        var musicEntitySchema = entity.entitySchema.extend({
            category: String
        });
        model = Service.createModel('_entity', musicEntitySchema, {
            name: 'text'
        }, '_entity');
    }

    return model;
};

和Service.js如下所示,

'use strict';
(function () {
    let dbHelper = require('./DbHelper');
    module.exports = {
        createModel: function (modelName, entityDef, indexObject, collection) {
            return dbHelper.createModel(modelName, entityDef, indexObject, collection);
        }

dbHelper.js

createModel: function (modelName, schemaObject, indexObject, collection) {
            var modelSchema = new mongoose.Schema(schemaObject);
            if (indexObject) {
                modelSchema.index(indexObject);
            }
            return collection ? mongoose.model(modelName, modelSchema, collection) : mongoose.model(modelName, modelSchema);
        }

但上述扩展方式会产生错误,因为 entity.entitySchema.extend不是函数

0 个答案:

没有答案