繁体   English   中英

java 脚本中的“module.exports”和“exports.module”有什么区别

[英]What is the difference between "module.exports" and "exports.module" in java script

我创建了一个名为“Product”的 Model 和下面的代码

const Sequelize = require('sequelize');

const sequelize = require('../util/database');

const Product = sequelize.define('product',{
    id: {
        type: Sequelize.INTEGER,
        autoincrement: true,
        allowNull: false,
        primaryKey: true
    },
    title: Sequelize.STRING,
    price: {
        type: Sequelize.DOUBLE,
        allowNull: false
    },
    imageUrl:{
        type: Sequelize.STRING,
        allowNull: true
    },
    description: {
        type: Sequelize.STRING,
        allowNull: true
    }
});

exports.module = Product;

我收到以下错误

Product.create is not a function

如果我替换exports.module = Product; module.exports = Product; , 它工作得很好。

任何人都可以澄清两者之间的区别

module.exports是用于从 CommonJS 模块中导出值的属性。

exportsmodule.exports的简写。

exports.module与 module.exports.module 相同,它在导出的module.exports.module上创建一个名为module的值。


因此,如果您有:

module.exports = Product;

然后你会匹配它:

const Product = require('./myModule');

但如果你有:

exports.module = Product;

然后你会:

const myModule = require('./myModule');
const Product = myModule.module;

exports.module基本上是一个错误,使代码混乱。 不要那样做。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM