我试图调用一个简单的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
});
})
}
};
答案 0 :(得分:0)
不得不改变
const db = require('../models/index').db;
到
const db = require('../models/index');
并添加const sequelize = require('sequelize');