[英]Mongoose ODM model creation problems
I am trying to create models in mongoose using an express app. 我正在尝试使用快速应用程序在mongoose中创建模型。 the error i am getting is the following:
我得到的错误如下:
Did you try nesting Schemas? You can only nest using refs or arrays.
My Setup is the following: 我的设置如下:
The Schemas module has an entry point index.js which has the following Schemas模块有一个入口点index.js,它具有以下内容
module.exports = require('./lib/index');
The above required index file(./lib/index.js) has: 上面要求的索引文件(./ lib / index.js)有:
module.exports = { InstituteSchema: require('./Schemas/Institute') }
The above Institute file(./Schemas/Institute) has the following: 上述研究所文件(./ Schemas / Institute)具有以下内容:
var mongoose = require('mongoose'); var Schema = mongoose.Schema; var RegistrySchema = new Schema({ name: String, privileges: [String] }); module.exports = RegistrySchema;
The following is an extract of my express app: 以下是我的快递应用程序的摘录:
var express = require('express'); var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/Project'); var http = require('http'); var path = require('path'); var routes = require('./routes'); var RegistrySchema = require('Schemas').RegistrySchema; var RegistryModel = mongoose.model('Registry', RegistrySchema);
however when I run Express i get the following stack trace: 但是当我运行Express时,我得到以下堆栈跟踪:
/usr/local/bin/node app.js
/Users/user/Documents/WebstormProjects/Project/node_modules/mongoose/lib/schema.js:362 throw new TypeError('Undefined type at `' + path +
TypeError: Undefined type at `paths.name`
Did you try nesting Schemas? You can only nest using refs or arrays.
at Function.Schema.interpretAsType (/Users/user/Documents/WebstormProjects/Project/node_modules/mongoose/lib/schema.js:362:11)
at Schema.path (/Users/user/Documents/WebstormProjects/Project/node_modules/mongoose/lib/schema.js:305:29)
at Schema.add (/Users/user/Documents/WebstormProjects/Project/node_modules/mongoose/lib/schema.js:217:12)
at Schema.add (/Users/user/Documents/WebstormProjects/Project/node_modules/mongoose/lib/schema.js:212:14)
at new Schema (/Users/user/Documents/WebstormProjects/Project/node_modules/mongoose/lib/schema.js:73:10)
at Mongoose.model (/Users/user/Documents/WebstormProjects/Project/node_modules/mongoose/lib/index.js:293:14)
at Object.<anonymous> (/Users/user/Documents/WebstormProjects/Project/app.js:12:30)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
Process finished with exit code 8
I would suggest you have a models directory within which you have a file defined for each model. 我建议你有一个模型目录,你可以在其中为每个模型定义一个文件。 In the model file export the model itself.
在模型文件中导出模型本身。
In models/Registry.js - 在models / Registry.js中 -
module.exports = mongoose.model('Registry', RegistrySchema);
And in your app.js, just require all the model files - 在你的app.js中,只需要所有的模型文件 -
var fs = require('fs'),
modelPath = __dirname + '/models';
fs.readdirSync(modelPath).forEach(function(file) {
require(modelPath + '/' + file);
});
Have a look at the MEAN stack boilerplace structure - https://github.com/linnovate/mean/tree/master/app 看一下MEAN堆栈的锅炉结构 - https://github.com/linnovate/mean/tree/master/app
Also have a look at this article to understand how require works - http://openmymind.net/2012/2/3/Node-Require-and-Exports/ 另请查看本文以了解需要如何工作 - http://openmymind.net/2012/2/3/Node-Require-and-Exports/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.