简体   繁体   English

如何在Node.js中的控制器中实例化模型?

[英]How to instantiate model in controller in Nodejs?

I am trying to use the model object in node through sequelize module. 我正在尝试通过sequelize模块在节点中使用模型对象。

I have something like these: 我有这样的事情:

File structure:
models
  index.js
  user.js
controllers
  userController.js
routes
  route.js

========================== =========================

models/users.js
//generated through sequelize cli
'use strict';
module.exports = function(sequelize, DataTypes) {
  var user = sequelize.define('user', {
    name: DataTypes.STRING,
    job: DataTypes.STRING
  }, {
    classMethods: {
      associate: function(models) {
        // associations can be defined here
      }
    }
  });
  return user;
};

models/index.js // generated file through sequelize. models/index.js //通过sequelize生成文件。

controllers/userController.js

var model = require('../models/user');

var userController = function(){
    function createOne (req, res) {
         model.create({
             'name': 'John',
             'job':'manager'
         }).then(function(test){
             console.log(test);
         })
    }

    return {
        createOne: createOne        
    }
}

module.exports = userController;

When my route request userController.createOne() , I am getting model.create is not a function 当我的路线请求userController.createOne() ,我得到了model.create is not a function

I am not sure what the issue is and how to get model injected into the controller. 我不确定问题是什么以及如何将模型注入控制器。 Can anyone help me about it? 有人可以帮我吗? Thanks a lot! 非常感谢!

Update : 更新

my models/index.js //generated by sequelize cli

'use strict';

var fs        = require('fs');
var path      = require('path');
var Sequelize = require('sequelize');
var basename  = path.basename(module.filename);
var env       = process.env.NODE_ENV || 'development';
var config    = require(__dirname + '../config/config.json')[env];
var db        = {};

if (config.use_env_variable) {
  var sequelize = new Sequelize(process.env[config.use_env_variable]);
} else {
  var sequelize = new Sequelize(config.database, config.username, config.password, config);
}

fs
  .readdirSync(__dirname)
  .filter(function(file) {
    return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
  })
  .forEach(function(file) {
    var model = sequelize['import'](path.join(__dirname, file));
    db[model.name] = model;
  });

Object.keys(db).forEach(function(modelName) {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

Normally you define the models individually, then let the models/index.js properly stage these into the Sequelize object. 通常,您分别定义模型,然后让models/index.js适当地将它们暂存到Sequelize对象中。 Later: 后来:

const models = require('../models');

const User = models.User;

Then you can call User.create() and so on. 然后,您可以调用User.create()等。 The bare model definition file is not a complete package. 裸模型定义文件不是完整的软件包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM