gpt4 book ai didi

sql - 在 express 应用程序中续写原始查询

转载 作者:行者123 更新时间:2023-12-03 22:20:02 28 4
gpt4 key购买 nike

我正在尝试调用一个简单的 sequelize 原始查询来获取用户表中的所有用户,但出现错误。

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

我在这里做错了什么?

模型/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;

Controller /用户
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
});
})
}
};

最佳答案

不得不改变

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


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

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

关于sql - 在 express 应用程序中续写原始查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50672695/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com