Node.js 节点js将架构放在其他文件夹中
我已经创建了基本节点项目,在该项目中,我在模型文件夹中创建了模型文件夹,我有schema folder和index.js 在schema文件夹中有我项目的所有schema,在index.js中我写了一些类似的东西Node.js 节点js将架构放在其他文件夹中,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我已经创建了基本节点项目,在该项目中,我在模型文件夹中创建了模型文件夹,我有schema folder和index.js 在schema文件夹中有我项目的所有schema,在index.js中我写了一些类似的东西 var s1 = require('./schema/schema1'); var s2 = require('./schema/schema2'); module.export = function(app) { schema1= s1 ; schema2= s2
var s1 = require('./schema/schema1');
var s2 = require('./schema/schema2');
module.export = function(app) {
schema1= s1 ;
schema2= s2 ;
}
在控制器文件中,我像这样访问模型
var model = require('./models/');
model.schema1.find(function(err, data) {
})
但是上面的代码不起作用,它的give me errorTypeError:无法读取未定义的属性“find”
,但是当我在控制器中尝试类似的操作时
var schema1= require('../models/schema/schema1');
schema1.find(function(err,data){});
它的工作很好,但我想在我的项目中的第一个结构,我使用mongoose,express,node,mongodb
请帮助我,我不知道我做错了什么将您的架构传递到第二个文件在第一个文件中执行类似操作:
var mySchema = new Schema({
// my props
});
require('./app/anotherFolder.js')(mySchema)
您的
index.js
导出一个不返回任何内容的函数。或者:
var s1 = require('./schema/schema1');
var s2 = require('./schema/schema2');
module.exports = function (app) {
return {
schema1: s1,
schema2: s2
}
}
然后像这样要求:var model=require('./models/')()代码>(请注意(),它是一个函数,必须调用它才能获取返回的对象)
或者,如果您根本不使用app
参数:
var s1 = require('./schema/schema1');
var s2 = require('./schema/schema2');
module.exports = {
schema1: s1,
schema2: s2
}
然后您可以简单地var model=require('./models/')代码>(这里没有()
您在module.exports上也有输入错误,您忘记了s