2014-03-02 123 views
3

我正在嘗試使用快速應用程序在貓鼬中創建模型。 我得到的錯誤是:貓鼬ODM模型創建問題

Did you try nesting Schemas? You can only nest using refs or arrays. 

我的設置如下:

  1. 我有我使用NPM鏈接鏈接到我的快遞應用程序的模塊名稱架構
  2. Schemas模塊有一個入口點index.js,它具有以下內容

    module.exports = require('./lib/index'); 
    
  3. 上述所需的索引文件(./ LIB/index.js)具有:

    module.exports = { InstituteSchema: require('./Schemas/Institute') } 
    
  4. 上述研究所文件(./架構/研究所)具有以下:

    var mongoose = require('mongoose'); 
    var Schema = mongoose.Schema; 
    
    var RegistrySchema = new Schema({ 
    name: String, 
    privileges: [String] 
    }); 
    
    module.exports = RegistrySchema; 
    
  5. 下面是一個我明確的應用程序的提取物:

    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); 
    

但是當我跑快我碰到下面的堆棧跟蹤:

/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 
+0

需求是錯誤的 - require('Schemas')。InstituteSchema; 另外,您在/lib/index.js中需要的東西的方式稍後會讓您陷入困境。改用__dirname和path.join。 –

+0

你可以請更具體的,所以我可以理解事情如何工作,,,我是新的節點和一個特定的困惑點是「出口」正確的方式 – Fouad

回答

1

我建議你有一個模型目錄,在其中有一個爲每個模型定義的文件。在模型文件中導出模型本身。

在模型/ Registry.js -

module.exports = mongoose.model('Registry', RegistrySchema); 

而在你的app.js,只是要求所有的模型文件 -

var fs = require('fs'), 
    modelPath = __dirname + '/models'; 

fs.readdirSync(modelPath).forEach(function(file) { 
    require(modelPath + '/' + file); 
}); 

看一看平均堆boilerplace結構 - https://github.com/linnovate/mean/tree/master/app

也看看這篇文章,瞭解如何工作 - http://openmymind.net/2012/2/3/Node-Require-and-Exports/

+0

非常感謝...這是非常有用的 – Fouad