gpt4 book ai didi

javascript - module.filename 未定义(Sequelize,Node.js)

转载 作者:行者123 更新时间:2023-11-29 13:17:54 26 4
gpt4 key购买 nike

我在读this Sequelize 和 Node.js 教程。我在第 4 行收到以下错误:

TypeError:路径必须是字符串。收到未定义

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;
db.Sequelize = Sequelize;

module.exports = db;

似乎 module.filename 未定义。我不知道为什么会这样(我是 Node 的新手)并且与教程和我的应用程序的唯一区别是我不使用 express api(但 GraphQL)并且我的 sequelize 自动生成的文件夹位于 ../postgres/而不是 ../

编辑:

如果我运行教程 repo 并打印 module.filename 我会得到 /Users/Creece/Downloads/postgres-express-node-tutorial-master/server/models/index.js

现在,如果我手动分配 module.filename,如下所示:module.filename = '/Users/Creece/Downloads/express_graphql_hmr_article_boilterplate-master/server/postgres/models/index.js'; (在分配 basename 常量之前),我在 const model = ... 行上收到一个新的奇怪错误:

Error: Cannot find module '/Users/Creece/Downloads/express_graphql_hmr_article_boilerplate-master/server/dist/postgres/models/todo.js'

请注意,上述路径中的文件夹“dist”是 webpack 的(虽然不确定,但它不是我的)。

编辑 2:(关于我对@NikMartin 的回答的评论

这是 Sequelize ./postgres/models/todo.js 自动生成的文件:

module.exports = (sequelize, DataTypes) => {

const Todo = sequelize.define('Todo', {
title: {
type: DataTypes.STRING,
allowNull: false,
},
});



Todo.associate = (models) => {
Todo.hasMany(models.TodoItem, {
foreignKey: 'todoId',
as: 'todoItems',
});
};
return Todo;

};

解决方案(基于已接受的答案):

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 = {};

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
);
}

const models = [
require('./Todo')(sequelize, Sequelize),
require('./TodoItem')(sequelize, Sequelize),
];

models.forEach(model => {
db[model.name] = model;
});

models.forEach(model => {

if (db[model.name].associate) {
console.log("entered");
db[model.name].associate(db);
}
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

最佳答案

关于 module.filename 问题 - 您可以阅读此讨论或仅阅读评论:https://github.com/webpack/webpack/issues/1599#issuecomment-328540024

引用:

aprilmintacpineda commented

for those having this issue, it is also good to note that module.filename would be undefined if you bundled your files. In my case it was undefined and I fixed it by simply replacing it with __filename which yields the same outcome.

因此,您应该能够将 path.basename(module.filename); 替换为 path.basename(__filename);

但据我所知,它仍然是/dist 文件夹中的捆绑 js 文件,而您的模型位于/postgres/models 文件夹中。

我在这里看到两个选项:

1) 不自动加载模型,而是直接请求所有必要的模型。

假设此文件的路径是 /postgres/models/index.js 并且实际模型在同一个文件夹中未测试

const Sequelize = require('sequelize');
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
);
}

// removed autoloading block

// setting the list of used models
const models = [
require('./TodoItem')(sequelize, Sequelize),
require('./Comment')(sequelize, Sequelize)
];

// registering models by their names
models.forEach(model => {
db[model.name] = model;
});

// using registered models to set up relations
models.forEach(model => {
if (model.associate) {
model.associate(db);
}
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

这意味着灵 active 降低,但您的捆绑代码将开始使用捆绑模型。我想对于 dist 代码来说,需要一些“外部”(缺少内部模型)非配置 js 文件才能工作并不是最好的事情。

2) 不是从当前文件夹而是从定义的文件夹加载文件。

  • /postgres/models/index.js 从 models 文件夹移出到 /postgres/index.js (这样模型文件夹将只有实际的模型文件)
  • 从文件中删除basename
  • 使用 .readdirSync(__dirname + "/../postgres/models")
  • 代替 .readdirSync(__dirname)
  • 删除 (file !== basename) && 检查(因为没有必要)

无论哪种方式,替换模块的属性 (module.filename) 都不是解决方案。

关于javascript - module.filename 未定义(Sequelize,Node.js),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46392660/

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