错误,当我使用导出,但使用module.exports
如果我使用module.exports = mongoose.model('People', PersonSchema, 'People');
比下面的代码工作正常
People = require("../models/people"); People.find(function (err, docs) {});
但与exports = mongoose.model('People', PersonSchema, 'People');
得到错误在People.find()
说TypeError: Object #<Object> has no method 'find'
为什么?
这是因为module.exports
的值是require()
在其他模块中返回的值。 exports
只是为了方便module.exports
提供的module.exports
的参考副本。
当您只修改 (或“ 扩充 ”)导出对象时,两者都可以工作,因为它们都指向同一个对象。 但是,一旦你打算replace这个对象,你必须把replace设置为module.export
。
来自模块 (强调我的):
请注意,
exports
是对module.exports
的引用,因此仅适用于扩充 。 如果您要导出单个项目(如构造函数),则需要直接使用module.exports
。function MyConstructor (opts) { //... } // BROKEN: Does not modify exports exports = MyConstructor; // exports the constructor properly module.exports = MyConstructor;