TypeError: (intermediate value) is not a function at async Promise.all

时间:2021-07-07 15:11:07

标签: javascript node.js sequelize.js

我正在重构我的索引文件,该文件在 Sequelize 中创建我的所有模型以使用导入而不是 require 并切换到 Node v14 和 Sequelize v6 和模块。但我被这个奇怪的错误困住了:

TypeError: (intermediate value) is not a function at file:///C:/.../models/index.js:41:6 at async Promise.all (index 0)

这是发生它的代码块:

fs.readdir(__dirname, (err, files) => {
    console.log('in the readdir!!');
    if (err) {
        console.log({ err });
    } else {
        console.log('the else!');
        console.log({ files });
        const promise_arr = files
            .filter((file) => {
                return (
                    file.indexOf('.') !== 0 &&
                    file !== basename &&
                    file.slice(-3) === '.js'
                );
            })
            .map(async (file) => {
                // const model = sequelize['import'](path.join(__dirname, file));
                console.log({ file });
                console.log({ __dirname });
                return (
                    await import('file:///' + path.join(__dirname, file))
                )(sequelize, Sequelize.DataTypes);
            });
        console.log({ promise_arr });
        Promise.all(promise_arr)
            .then((models) => {
                models.forEach((model) => (db[model.name] = model));
                associateCB();
            })
            .catch((e) => console.log({ e }));
    }
});

function associateCB() {
    console.log('associateCB with: ', db);
    Object.keys(db).forEach((modelName) => {
        if (db[modelName].associate) {
            db[modelName].associate(db);
        }
    });
    console.log(1, { db });
}

错误表明它发生在这一行:)(sequelize, Sequelize.DataTypes);

1 个答案:

答案 0 :(得分:0)

看起来可能是分号问题?请参阅:Uncaught TypeError: (intermediate value)(...) is not a function

return (
    await import('file:///' + path.join(__dirname, file)); // <--- Add this semicolon
)(sequelize, Sequelize.DataTypes);