【发布时间】:2014-04-03 05:08:03
【问题描述】:
我正在尝试使用 express 应用在 mongoose 中创建模型。 我得到的错误如下:
Did you try nesting Schemas? You can only nest using refs or arrays.
我的设置如下:
- 我有一个模块名称 Schemas,我使用 npm link 链接到我的 Express 应用程序
-
Schemas 模块有一个入口点 index.js,它具有以下内容
module.exports = require('./lib/index'); -
上述所需的索引文件(./lib/index.js)有:
module.exports = { InstituteSchema: require('./Schemas/Institute') } -
上述研究所文件(./Schemas/Institute)有以下内容:
var mongoose = require('mongoose'); var Schema = mongoose.Schema; var RegistrySchema = new Schema({ name: String, privileges: [String] }); module.exports = RegistrySchema; -
以下是我的快递应用的摘录:
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);
但是,当我运行 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
【问题讨论】:
-
require 错误 - require('Schemas').InstituteSchema;此外,您在 /lib/index.js 中要求内容的方式稍后会让您遇到问题。改用 __dirname 和 path.join。
-
能否请您更具体一些,以便我了解事情的运作方式,我是 node 新手,一个特别的困惑点是“导出”正确的方式
标签: node.js express model mongoose schema