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

问题描述

我目前在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

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

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

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

10-24 19:41