在快递应用程序中查询原始查询

时间:2018-06-04 02:19:39

标签: sql postgresql express sequelize.js

我试图调用一个简单的sequelize原始查询来获取users表中的所有用户,但是我收到了错误。

无法读取属性'查询'未定义的

我在这里做错了什么?

模型/ index.js

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

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

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

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

db.sequelize = sequelize;

module.exports = db;

控制器/用户

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

module.exports = {

  getAllUsers(req, res){
    db.query('SELECT * FROM "Users" As Users', { type: sequelize.QueryTypes.SELECT})
    .then(result => {
      return res.status(201).send({
        result
      });
    })
  }
};

1 个答案:

答案 0 :(得分:0)

不得不改变

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

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

并添加const sequelize = require('sequelize');