express - GraphQl、Sequelize-CLi、模型打包器 - sequelize.import 不是函数

标签 express graphql sequelize.js sequelize-cli

我正在关注有关 sequelize-cli 的教程:https://andela.com/insights/using-graphql-and-sequelize/
我得到了一个错误:'sequelize.import 不是一个函数'
它来自models/index.js(它应该捆绑所有模型)

'use strict';

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

// Set DB Connection
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);
}

// Import model files in models folder and make table associations where present
fs
  .readdirSync(__dirname)
  .filter(file => {
    return (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;

我在网上搜索解决方案,但无论我看到它都不起作用。
有任何想法吗??

最佳答案

OK解决了!
我还没有意识到

  .forEach(file => {
    const model = sequelize['import'](path.join(__dirname, file));
    db[model.name] = model;
  });

位于同一位置,因此正确答案是:
  .forEach(file => {
    db[model.name] = file
  });

关于express - GraphQl、Sequelize-CLi、模型打包器 - sequelize.import 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63063381/

相关文章:

node.js - 如何从中间件获取api结果到路由

graphql - 抑制 GraphQL 响应中的重复项

javascript - 在字符串化为 JSON 之前修改实体

node.js - PassportJS(Local) 在序列化用户后,Sequelize 和 Express 停留在挂起状态

javascript - 重定向相对路径 Express.js 出错

javascript - NodeJS 分配变量不起作用

javascript - Node.js 表达嵌套路由

graphql - 我的光标来自哪里?

graphql - 变量 $varname 未出现在任何 graphQL 查询中

node.js - 如何在 Sequelize ORM 映射到 MySQL 数据库中使用 COUNT DISTINCT CASE 表达式?