Mongoose & 的文件结构节点项目 [英] File Structure of Mongoose & NodeJS Project

查看:27
本文介绍了Mongoose & 的文件结构节点项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在我的 Mongoose/NodeJS 应用程序的/models/models.js 文件中有我的所有模型(架构定义).

I currently have all my models (Schema definitions) in the /models/models.js file for my Mongoose/NodeJS application.

我想将它们分解成不同的文件,例如:user_account.js、profile.js 等.但是我似乎无法这样做,因为我的控制器损坏并报告找不到模块"一旦我将这些课程分开.

I'd like to break these apart into different files as such: user_account.js, profile.js, etc. However I cannot seem to do so as my controllers break and report back "cannot find module" once I pull these classes apart.

我的项目结构如下:

/MyProject
  /controllers
    user.js
    foo.js
    bar.js
    // ... etc, etc
  /models
    models.js
  server.js

我的models.js 文件的内容如下所示:

The contents of my models.js file looks like this:

var mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    ObjectId = Schema.ObjectId;

mongoose.connect('mongodb://localhost/mydb');

var UserAccount = new Schema({
    user_name       : { type: String, required: true, lowercase: true, trim: true, index: { unique: true } }, 
    password        : { type: String, required: true },
    date_created    : { type: Date, required: true, default: Date.now }
}); 

var Product = new Schema({
    upc             : { type: String, required: true, index: { unique: true } },
    description     : { type: String, trim: true },
    size_weight     : { type: String, trim: true }
});

我的 user.js 文件(控制器)如下所示:

My user.js file (controller) looks like this:

var mongoose    = require('mongoose'), 
    UserAccount = mongoose.model('user_account', UserAccount);

exports.create = function(req, res, next) {

    var username = req.body.username; 
    var password = req.body.password;

    // Do epic sh...what?! :)
}

如何将架构定义分解为多个文件并从我的控制器中引用它?当我确实引用它时(在架构位于新文件中之后)我收到此错误:

How can I break the schema definition into multiple files and also reference it from my controller? When I do reference it (after the schema is in a new file) I get this error:

*错误:尚未为模型user_account"注册架构.*

*Error: Schema hasn't been registered for model "user_account".*

想法?

推荐答案

这是一个示例 app/models/item.js

var mongoose = require("mongoose");

var ItemSchema = new mongoose.Schema({
  name: {
    type: String,
    index: true
  },
  equipped: Boolean,
  owner_id: {
    type: mongoose.Schema.Types.ObjectId,
    index: true
  },
  room_id: {
    type: mongoose.Schema.Types.ObjectId,
    index: true
  }
});

var Item = mongoose.model('Item', ItemSchema);

module.exports = {
  Item: Item
}

要从 app/controllers/items.js 中的项目控制器加载它,我会这样做

To load this from an item controller in app/controllers/items.js I would do

  var Item = require("../models/item").Item;
  //Now you can do Item.find, Item.update, etc

换句话说,在您的模型模块中定义架构和模型,然后仅导出模型.使用相对的 require 路径将模型模块加载到控制器模块中.

In other words, define both the schema and the model in your model module and then export just the model. Load your model modules into your controller modules using relative require paths.

要建立连接,请在服务器启动代码(server.js 或其他代码)中尽早处理.通常,您需要从配置文件或环境变量中读取连接参数,如果未提供配置,则默认为开发模式 localhost.

To make the connection, handle that early in your server startup code (server.js or whatever). Usually you'll want to read the connection parameters either from a configuration file or from environment variables and default to development mode localhost if no configuration is provided.

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost');

这篇关于Mongoose & 的文件结构节点项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆