【问题标题】:How to 'require' a database schema in nodejs如何在 nodejs 中“要求”数据库模式
【发布时间】:2012-07-25 17:55:36
【问题描述】:

在 nodejs 中 require 一只猫鼬 Schema 的最佳方式是什么?

最初我在 app.js 文件中有这些,但随着模型的增多,它变得有点大而且笨拙。

现在我想将它们移动到models 文件夹并使用Model = require('./models/model') 将它们导入到 app.js 中

如何让Model 填充实际模型?

exports = mongoose.model(...) 失败并给了我一个空白对象;exports.model = mongoose.model(...) 要求我执行 Model.model 才能访问它——这些都不是所需的行为)

===

编辑1

所以基本上我已经采取了

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

var UserSchema = new Schema({
  username: String,
  password: String,
  first_name: String,
  last_name: String,
  email: String
});
User = mongoose.model('User', UserSchema);

并将其放入./models/user.js

我如何获得它,使其相当于在 app.js 中拥有它?

【问题讨论】:

    标签: node.js model mongoose require


    【解决方案1】:

    在您的 app.js 服务器文件中,包含 model.js 文件,如下所示:

    var Model = require('./models/model');  //whatever you want to call it
    

    然后你可以像这样在你的服务器文件中实例化它:

    //Initiate the  Business API endpoints
    var model = new Model(mq, siteConf);
    model.getUser(id, function() {
        // handle result
    });
    

    ----

    然后在您的文件中,您将其放在名为 model.js 的 models 文件夹中(或任何您想要的),您可以像这样进行设置:

    var mongoose = require('mongoose'); 
    
    //MongoDB schemas
    var Schema = mongoose.Schema;
    
    var User = new Schema({
      username: String,
      password: String,
      first_name: String,
      last_name: String,
      email: String
    });
    var UserModel = mongoose.model('User', User);
    
    // your other objects defined ...
    
    
    module.exports = function(mq, siteConf) {
    //MongoDB
    mongoose.connect(siteConf.mongoDbUrl);
    
    
    // ------------------------
    // READ API
    // ------------------------
    
    // Returns a user by ID
    function getUser(id, found) {
        console.log("find user by id: " + id);
        UserModel.findById(id, found);
    }
    
    // Returns one user matching the given criteria
    // mainly used to match against email/login during login
    function getUserByCriteria(criteria, found) {
        console.log("find user by criteria: " + JSON.stringify(criteria));
        UserModel.findOne(criteria, found);
    }
    
    
    
        // more functions for your app ...
    
    
    
    return {
        'getUser': getUser, 
                'getUserByCriteria': getUserByCriteria
       };
    };
    

    【讨论】:

    • 我不太确定这如何解决我的问题。现在我什至还没有在我的模型上使用方法(非常简单的数据库操作,使用我从mongoose.model 得到的值;像User.find() 这样的东西)
    • *编辑的问题更具体地说明了我想要做什么。感谢您的回答:)
    • 针对您的问题编辑了更多答案,并消除了其他提示以减少混淆。你想要的是一个更干净的 app.js 文件,所以我建议你创建一个新文件并将其放在 /lib/ 目录中,并将该文件包含在 app.js 中。然后实例化该类并调用其函数,如图所示。
    • 你会看到我 return 类的函数,我创建函数作为你提到的函数的包装器,但也像 UserModel.find(..) 一样调用它们,但我只是将 var 命名为顶部不同。使用 found 只是一个回调函数,所以我的代码保持异步,这就是您想要对 Node.js 执行的操作,因此您的应用程序是非阻塞的。
    • 我明白了。那么为了异步数据库调用,每个调用都必须包装在自己的函数中?
    猜你喜欢
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    • 2014-09-26
    • 1970-01-01
    • 1970-01-01
    • 2012-01-17
    相关资源
    最近更新 更多