Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.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/6/mongodb/13.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
外部模块中的架构在Node.js中不起作用_Node.js_Mongodb_Mongoose - Fatal编程技术网

外部模块中的架构在Node.js中不起作用

外部模块中的架构在Node.js中不起作用,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我非常头痛,试图通过一个模块将一些常见的模式定义共享给代码库中的所有其他模块 我有一个myproj_模式模块,其中包含以下两个模式: var mongoose = require('mongoose'), util = require("util"), Schema = mongoose.Schema; var BaseProfileSchema = function() { Schema.apply(this, arguments); this.add({

我非常头痛,试图通过一个模块将一些常见的模式定义共享给代码库中的所有其他模块

我有一个myproj_模式模块,其中包含以下两个模式:

var mongoose = require('mongoose'),
    util = require("util"),
    Schema = mongoose.Schema;

var BaseProfileSchema = function() {
    Schema.apply(this, arguments);

    this.add({
        _user: {type: Schema.Types.ObjectId, ref: 'User', required: true},
        name: {type: String, required: true},
        bio: {type: String, required: true},
        pictureLink: String
    });

};
util.inherits(BaseProfileSchema, Schema);

module.exports = BaseProfileSchema;

导师也在另一个文件中定义

我对这两者的单元测试都在schemas模块中工作

当我安装此模块并尝试使用

Entrepreneur = db.model('Entrepreneur', entrepreneurSchema),
我得到以下错误:

类型错误:
路径处未定义类型。导师
你试过嵌套模式吗?只能使用参照或阵列进行嵌套

如果我在本地模块中使用相同的代码,那么没有问题。 如果我直接在require中引用模式文件(例如require(“../node\u modules/myproj\u schemas/models/ent\u schema”),那么我会得到错误

我相当确定它没有像之前那样崩溃,但我已经放弃了所有的更改,它仍然不工作

我画的是一个完全空白,任何建议都将被感激地接受

编辑:

我创建了一个新的架构模块。它有一个架构:

var mongoose = require('mongoose');

var userSchema = new mongoose.Schema({
    email: String
});

module.exports = userSchema;
当打包在一个模块中并将npm安装到其他模块时,这也会失败


在OS X上运行

为什么不像这样为公共模式创建一个mongoose插件

我个人创建了一个单独的“common”项目,使用init方法向mongodb注册所有模型,并在需要访问模型的任何应用的app.js文件中调用init方法

  • 创建共享项目-按照标准流程创建新节点项目
  • package.json-在共享项目中,将
    package.json
    文件设置为包含以下条目:

    "main": "index.js"
    
  • 添加模型-在共享项目中创建一个新的
    模型
    文件夹,以包含所有mongoose架构和插件。将userSchema文件添加到模型文件夹中,并将其命名为
    user.js

    var mongoose = require('mongoose');
    
    var userSchema = new mongoose.Schema({
        email: String
    });
    
    module.exports = mongoose.model('User', userSchema);
    
  • index.js-然后在项目的根
    index.js
    文件中创建一个可供应用程序使用的共享对象,公开模型和
    init
    方法。有很多方法可以编写此代码,但我是这样做的:

    function Common() {
        //empty array to hold mongoose Schemas
        this.models = {};
    }
    
    Common.prototype.init = function(mongoose) {
        mongoose.connect('your mongodb connection string goes here');
        require('./models/user');
        //add more model references here
    
        //This is just to make referencing the models easier within your apps, so you don't have to use strings. The model name you use here must match the name you used in the schema file
        this.models = {
            user: mongoose.model('User')
        }
    }
    
    var common = new Common();
    
    module.exports = common;
    
  • 引用您的
    公共项目
    项目
    -如果您想引用您的共享项目,请在应用程序中的
    包.json
    文件中添加对共享项目的引用,并将其命名为
    公共项目
    。我个人使用GitHub存储项目并引用存储库路径sitory是私有的,我必须在路径中使用密钥,这在GitHub支持站点上有介绍
  • Init应用程序中的模型-在应用程序的启动脚本中(假设在本例中为
    app.js
    ),添加对
    common
    项目的引用,并调用
    Init
    方法连接到mongodb服务器并注册模型

    //at the top, near your other module dependencies
    var mongoose = require('mongoose')
      , common = require('common');
    
    common.init(mongoose);
    
  • 在应用程序中的任何位置使用模型-现在mongoose已经建立了连接池,并且模型已经注册,您可以在应用程序中的任何类中使用模型。例如,假设您有一个页面显示有关
    用户的信息
    ,您可以这样做(未经测试的代码,仅编写此示例):

  • 编辑 很抱歉,我没有看到您从一个模式继承另一个模式的行。正如其他答案之一所述,mongoose已经提供了
    插件的概念。在上面的示例中,您可以这样做:

    在通用模块中的“/models/base profile plugin.js”下

    module.exports = exports = function baseProfilePlugin(schema, options){
    
        //These paths will be added to any schema that uses this plugin
        schema.add({
            _user: {type: Schema.Types.ObjectId, ref: 'User', required: true},
            name: {type: String, required: true},
            bio: {type: String, required: true},
            pictureLink: String
        });
    
        //you can also add static or instance methods or shared getter/setter handling logic here. See the plugin documentation on the mongoose website.
    }
    
    在通用模块中,在“/models/enterpriser.js”下

    var mongoose    = require('mongoose')
      , basePlugin  = require('./base-profile-plugin.js');
    
    var entrepreneurSchema   = new mongoose.Schema({
        mentors: {type: Schema.Types.ObjectId, ref: 'Mentor'}
    });
    
    entrepreneurSchema.plugin(basePlugin);
    
    module.exports = mongoose.model('Entrepreneur', entrepreneurSchema);
    

    基本上,您要寻找的是模式继承,有一个名为mongoose extend的项目实际上解决了您的问题,您可以决定是否要实现它,或者查看代码并制定自己的解决方案

    只需使用npm安装即可

    $ npm install mongoose-schema-extend
    
    这就是它的工作原理:

    var mongoose = require('mongoose'),
        extend = require('mongoose-schema-extend');
    var Schema = mongoose.Schema;
    
    var PersonSchema = new Schema({
      name : String
    }, { collection : 'users' });
    
    var EmployeeSchema = PersonSchema.extend({
      department : String
    });
    
    var Person = mongoose.model('Person', PersonSchema),
        Employee = mongoose.model('Employee', EmployeeSchema);
    
    var Brian = new Employee({
      name : 'Brian Kirchoff',
      department : 'Engineering'
    });
    

    关于

    这是一个非常有用的答案,但没有解决这个问题。在编辑中,你可以看到我遇到了非扩展模式的问题。我越来越确信这个问题是环境问题,但还没有机会在另一台机器上证明它。感谢你提供了这样一个完整的答案。自从遇到这个问题,我更加感动这类解决方案不需要我在模块之间共享模式。我仍然不清楚问题发生的原因,但我开始认为这是环境问题,可能一些缓存清除会有所帮助。我将接受这个答案——不是因为它实际上直接回答了我的问题,而是因为se我认为将其放在最顶端将为社区提供最大的服务。再次感谢您的努力。我没有真正做到这一点,必须使用
    proxyquire
    覆盖架构定义模块中使用的Mongoose实例。可以,但看起来非常脏。我不知道这在当时是否有效,但今天,如果您通过“mongoose”模块和do.connect()”连接到该模块,连接将是该模块的本地连接。因此,您在models目录中定义的模型需要另一个“mongoose”,但此模型未连接。因此,您在“init”中传递的mongoose上的模型将不可用“@tdecs我也在想这个问题。我来到这个线程,并认为它对我也很有用,但我得到了“MissingSchemaError:Schema尚未注册模型”错误。你找到解决方案了吗?
    $ npm install mongoose-schema-extend
    
    var mongoose = require('mongoose'),
        extend = require('mongoose-schema-extend');
    var Schema = mongoose.Schema;
    
    var PersonSchema = new Schema({
      name : String
    }, { collection : 'users' });
    
    var EmployeeSchema = PersonSchema.extend({
      department : String
    });
    
    var Person = mongoose.model('Person', PersonSchema),
        Employee = mongoose.model('Employee', EmployeeSchema);
    
    var Brian = new Employee({
      name : 'Brian Kirchoff',
      department : 'Engineering'
    });