当前位置: 首页 > 面试题库 >

Mongoose和NodeJS项目的文件结构

翟修永
2023-03-14
问题内容

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

我想将它们分解成不同的文件,例如:user_account.js,profile.js等。但是,我似乎无法这样做,因为一旦我将这些类分开,我的控制器就会分解并报告“
找不到模块 ”。

我的项目结构如下:

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

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

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文件(控制器)如下所示:

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?! :)
}

如何将模式定义分成多个文件,并从我的控制器中引用它?当我确实引用它时(在新文件中之后),我得到这个错误:

错误:尚未为模型“ 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我会这样做

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

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

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

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


 类似资料:
  • 问题内容: 我注意到Node.js项目通常包含以下文件夹: / libs,/ vendor,/ support,/ spec,/ tests 这些到底是什么意思?它们之间有什么区别,我应该在哪里包含引用的代码? 问题答案: 关于您提到的文件夹: 通常用于自定义 或 包含第三方库(使用git作为源代码管理时添加为git子模块) 包含BDD测试规范。 包含应用程序的单元测试(使用测试框架,请参见 此处

  • 前言 这一篇,我们将接着上篇来完成创建项目文件、目录结构。 回顾 先回顾一下现在项目有哪些东西了: . ├── app │   ├── app.vue │   ├── common │   │   ├── img │   │   ├── js │   │   └── scss │   ├── index.html │   ├── index.js │   ├── router

  • 我们看一下全局的文件结构: ▸ build/ // 编译用到的脚本 ▸ config/ // 各种配置 ▸ dist/ // 打包后的文件夹 ▸ node_modules/ // node第三方包 ▸ src/ // 源代码 ▸ static/

  • 1. 前言 在上一小节中,我们介绍了 Vue-Cli 初始化项目,本小节我们一起来分析以下 Vue-Cli 创建项目的文件结构。 2. 目录结构 首先我们先看以下用 Vue-Cli 创建项目的整体结构: 项目相关的代码,我们都放在 src 的文件夹中,该文件夹中的结构如下: assets 是资源文件夹,通常我们会把图片资源放在里面。 components 文件夹通常会放一些组件。 router 文

  • 问题内容: IntelliJ IDEA中Java项目可接受的文件夹结构是什么? 多个来源建议采用以下结构: 我知道这以前曾经奏效,但现在却在抱怨 显然],不允许将其作为程序包名称。我不明白为什么有时可以接受,有时不能接受。有人可以在IntelliJ IDEA中的Java项目中提供可接受的项目文件夹结构的完整示例吗? 问题答案: 这是Maven项目的基本文件夹结构。IntelliJ通常会识别出此情况

  • 我知道这种方法以前起过作用,但现在它正在抱怨 显然,不允许作为包名。我不明白为什么有时可以接受,有时不能接受。有人能在IntelliJ IDEA中提供一个Java项目中可接受的项目文件夹结构的完整示例吗?