Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Mongoose架构继承:entity.entitySchema.extend不是函数_Javascript_Node.js_Mongodb_Mongoose - Fatal编程技术网

Javascript Mongoose架构继承:entity.entitySchema.extend不是函数

Javascript Mongoose架构继承:entity.entitySchema.extend不是函数,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我定义了一个基本模式,如下所示,并使用 testEntitySchema.js var entitySchema = { entityId: { type: String }, name: { type: String }, description: { type: String }, type: { type: String }, subcategory: {

我定义了一个基本模式,如下所示,并使用

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;
};
'use strict';
(function () {
    let dbHelper = require('./DbHelper');
    module.exports = {
        createModel: function (modelName, entityDef, indexObject, collection) {
            return dbHelper.createModel(modelName, entityDef, indexObject, collection);
        }
Service.js如下所示:

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;
};
'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不是一个函数

如果不在testEntitySchema.js中使用
Schema
var entitySchema=new Schema({…
,稍后将在DbHelper中执行此操作),则无法使用extend。解决了这个问题,我在你的代码中遇到了多个其他错误。你能解释一下你试图用这个复杂的结构实现什么吗?@TalhaAwan我应该如何修改上面的内容?@TalhaAwan有什么想法吗?如果你愿意,我可以通过保留一个简单的结构来发布一个带有工作示例的答案。或者你愿意使用当前的结构吗?因为我不知道你想用它实现什么,@TalhaAwan请发布一个示例。你的联系电子邮件是什么?