GraphQl,Sequelize-CLi,模型捆绑器 - sequelize.import 不是函数

Posted

技术标签:

【中文标题】GraphQl,Sequelize-CLi,模型捆绑器 - sequelize.import 不是函数【英文标题】:GraphQl , Sequelize-CLi, models bundler - sequelize.import is not a function 【发布时间】:2020-11-13 17:14:11 【问题描述】:

我正在关注关于 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;

我在网上搜索解决方案,但我看到的都不起作用。

有什么想法吗??

【问题讨论】:

【参考方案1】:

好的解决了!

没想到

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

在同一个位置,所以正确答案是:

  .forEach(file => 
    db[model.name] = file
  );

【讨论】:

以上是关于GraphQl,Sequelize-CLi,模型捆绑器 - sequelize.import 不是函数的主要内容,如果未能解决你的问题,请参考以下文章

nodejs: express sequelize-cli

GraphQL 和 graphql-sequelize-schema-generator

luogu P4156 [WC2016]论战捆竹竿

在 Node 14 中使用 sequelize-cli,类型为:module

sequelize(和 sequelize-cli)queryInterface.createTable 在 PostgreSQL 上,PK id 类型为 UUID 和 defaultValue:Se

「WC2016」论战捆竹竿