gpt4 book ai didi

node.js - NodeJS 无法导入 Sequelize.js 模型(ES6)

转载 作者:太空宇宙 更新时间:2023-11-04 03:01:57 25 4
gpt4 key购买 nike

我正在使用 Express 和 Seqeulize 运行 NodeJS,并且有一个文件 controllers/rooms.jsmodels/room.js 导入 Room。

import Room from '../models'

export function list(req, res) {
return Room
.findAll()
.then((rooms) => res.status(200).send(rooms))
.catch((error) => res.status(400).send(error))
}

波纹管是models/room.js(同一目录下还有sequelize-cli生成的index.js文件)

'use strict'

export default (sequelize, DataTypes) => {

const Room = sequelize.define('Room', {
name: DataTypes.STRING
})

return Room
}

我有一条路线 app.get('/rooms', list),但是当我访问此路线时,出现此错误:

TypeError: _models2.default.findAll is not a function
at list (/Users/matis/Documents/apps/node-docker-test/app/database/controllers/rooms.js:21:10)
at Layer.handle [as handle_request] (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/layer.js:95:5)
at /Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/index.js:335:12)
at next (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/index.js:275:10)
at expressInit (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/middleware/init.js:40:5)
at Layer.handle [as handle_request] (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/index.js:317:13)
at /Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/index.js:335:12)
at next (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/index.js:275:10)
at query (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/middleware/query.js:45:5)
at Layer.handle [as handle_request] (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/index.js:317:13)
at /Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/index.js:335:12)
at next (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/index.js:174:3)
at Function.handle (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/application.js:174:10)

我确定我的导入/导出搞砸了,但我不知道是怎么回事。

models/index.js 文件如下

'use strict'

import { readdirSync } from 'fs'
import { basename as _basename, join } from 'path'
import Sequelize from 'sequelize'
const basename = _basename(__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], config)
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config)
}

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

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

db.sequelize = sequelize
db.Sequelize = Sequelize

export default db

当我这样调用它时它就起作用了:返回 Room.Room.findAll()...因此我可以将导入重命名为:从“../models”导入模型并这样调用它:返回 models.Room.findAll()...

但是为什么我不能直接调用它return Room.findAll()...,导入应该如何制定??

最佳答案

假设您已设置 babel 来使用 ES6 导入,您可以尝试使用此方法在 ES6 中导出 Sequelize 模型。

//数据库配置文件

import Sequelize from 'sequelize';

export const sequelize = new Sequelize(
config.database.name,
config.database.user,
config.database.password,
{
host: config.database.host,
dialect: config.database.dialect,
pool: config.database.pool,
operatorsAliases: false
}
);

//模型

import Sequelize from 'sequelize';
import { sequelize } from '../database/db';

const User = sequelize.define(
'table_name',
{
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: {
type: Sequelize.STRING,
allowNull: false
},
email: {
type: Sequelize.STRING,
allowNull: false
},
password: {
type: Sequelize.STRING,
allowNull: false
}
},
{ freezeTableName: true }
);

export default User;

关于node.js - NodeJS 无法导入 Sequelize.js 模型(ES6),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52987837/

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