【问题标题】:How to access constant variable in controller in nodejs如何在nodejs中访问控制器中的常量变量
【发布时间】:2017-07-29 01:07:46
【问题描述】:

我在模型文件中定义了一个常量变量,并尝试在将数据插入表时访问此常量变量,然后它显示未定义。我正在使用 sequlaize ORM 和 nodejs。我想为状态定义三个值,例如 Active = 1、Inactive = 2、Deleted = 3,或者是否有任何其他解决方案来定义它。我正在使用续集 ORM 4+ 版本。

Model code is here

var Sequalize = require('sequelize');
var sequalize = require('../../config/db_config');

const ACTIVE = '1';
const INACTIVE = '2';
const DELETED = '3';

const User = sequalize.define('user',{
    user_id_pk : {
        type : Sequelize.INTEGER,
        allowNull : false,
        primaryKey : true,
        autoIncrement : true
    },
    user_type : Sequelize.INTEGER,
    first_name : Sequelize.STRING,
    middle_name : Sequelize.STRING,
    last_name : Sequelize.STRING,
    password : Sequelize.STRING,
    email : Sequelize.STRING,
    mobile_no : Sequelize.STRING,
    is_email_verified : {
        type:Sequalize.INTEGER,
        defaultValue : 0
    },
    gender : Sequelize.STRING,
    profile_image: Sequelize.STRING,
    dob : Sequelize.DATE,
    is_active : Sequelize.STRING,
    created_date : Sequelize.DATE,
    created_by : Sequelize.INTEGER,
    updated_date : Sequelize.DATE,
    updated_by : Sequelize.INTEGER,
},{
    timestamps: false,
    paranoid: true,
    underscored: true,
    freezeTableName: true,
    tableName: 'user',
    createdAt:'created_date',
    updatedAt:'updated_date'
});

User.sync({force:false}).then(() => {
    console.log('Table is created!');    
}).catch(err => {
    console.log('An error occur when table is created!');
});
module.exports = User;

Controller code is here 

userController.saveUser = function(req,res){
    console.log(user.ACTIVE);return false;
    user.findOrCreate({
        where:{
            first_name: req.body.first_name,
            email: req.body.email,
            boltt_code: req.body.boltt_code
        },
        defaults:{
            first_name: req.body.fihhrst_name,
            middle_name : 'Rajeev',
            last_name : 'Varshney',
            email: req.body.email,
            boltt_code: req.body.boltt_code,
            is_active : '1',
        }
    })
    .spread( function(user, created) {
        var msg = '';
        if(created){
            msg = 'User inserted successfully!';
        }else{
            msg = 'User already exist!';
        }
        res.status(200).send({error : false,message : msg2, data : user });
    })
    .catch((err) => {
        console.log('Oops something wrong! ' + err);
    });
};

【问题讨论】:

  • 这个问题和MySQL有什么关系?
  • 我确定您使用它,但我在问题中看不到任何与 MySQL 相关的内容。您应该删除 MySQL 标签并添加 sequelize.js 标签。如果您仔细观察,库的名称是Sequelize,而不是您拼错的“Sequalize”。然后你想知道为什么代码不起作用......
  • 我的意思是,在代码中。它充满了Sequalize.INTEGER。即使Google knows也应该是Sequelize.INTEGER

标签: node.js sequelize.js


【解决方案1】:

如果你想在同一个文件中使用这些变量,你只需要声明它们并使用它。 如果你想从另一个文件访问它们,你必须导出变量:

const ACTIVE = '1';
const INACTIVE = '2';
const DELETED = '3';
exports.ACTIVE = ACTIVE;
exports.INACTIVE = INACTIVE;
exports.DELETED = DELETED;

现在您可以对文件进行 require 并访问变量。

【讨论】:

    【解决方案2】:
    We can initialize constant variable to model instance after creating the model instance. 
    
    const ACTIVE = 1;
    const INACTIVE = 2;
    const DELETED = 3;
    
    const user = sequelize.define('user',{..},{
    
    });
    
    User.ACTIVE = ACTIVE;
    User.INACTIVE = INACTIVE;
    User.DELETED = DELETED;
    
    module.exports = User;
    

    【讨论】:

      【解决方案3】:

      ModelCore.js 文件中,不仅导出用户,还导出您的常量:

      // ModelCore.js
      var Sequalize = require('sequelize');
      var sequalize = require('../../config/db_config');
      
      const ACTIVE = '1';
      const INACTIVE = '2';
      const DELETED = '3';
      
      const User = sequalize.define('user', {...});
      
      module.exports = {
          User,
          CODES: {
              ACTIVE,
              INACTIVE,
              DELETED
          }
      }
      

      现在将其导入您的 ControllerCode.js

      // ControllerCode.js
      const modelCore = require('./path/to/ModelCore');
      
      const user = modelCore.User;
      const {CODES} = modelCore;
      
      //Using codes now should work
      console.log(CODES.ACTIVE);
      console.log(CODES.INACTIVE);
      console.log(CODES.DELETED);
      
      // Rest of your Controller code...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-22
        • 2016-10-14
        • 2012-06-26
        • 2013-05-28
        • 1970-01-01
        • 2015-02-24
        • 1970-01-01
        相关资源
        最近更新 更多