Node.js 使用导出但使用module.exports时出错
如果我使用Node.js 使用导出但使用module.exports时出错,node.js,Node.js,如果我使用module.exports=mongoose.model('People',PersonSchema,'People')低于以下代码工作正常 People = require("../models/people"); People.find(function (err, docs) {}); 但是使用exports=mongoose.model('People',PersonSchema,'People')getERRORatPeople.find()sayingTypeError
module.exports=mongoose.model('People',PersonSchema,'People')代码>低于以下代码工作正常
People = require("../models/people");
People.find(function (err, docs) {});
但是使用exports=mongoose.model('People',PersonSchema,'People')
getERRORatPeople.find()
sayingTypeError:Object#没有方法“find”
为什么?这是因为module.exports
的值是由其他模块中的require()
返回的值<代码>导出
只是模块的参考副本。导出
是为了方便而提供的
当您仅修改(或“扩充”)导出对象时,两者都将起作用,因为它们都引用同一对象。但是,一旦要替换对象,必须将替换设置为module.export
来自(我的):
请注意,导出
是对模块的引用。导出
使其仅适用于增强。如果要导出单个项(如构造函数),则需要使用模块。直接导出
function MyConstructor (opts) {
//...
}
// BROKEN: Does not modify exports
exports = MyConstructor;
// exports the constructor properly
module.exports = MyConstructor;
这是因为
module.exports
的值是由其他模块中的require()
返回的值<代码>导出只是模块的参考副本。导出
是为了方便而提供的
当您仅修改(或“扩充”)导出对象时,两者都将起作用,因为它们都引用同一对象。但是,一旦要替换对象,必须将替换设置为module.export
来自(我的):
请注意,导出
是对模块的引用。导出
使其仅适用于增强。如果要导出单个项(如构造函数),则需要使用模块。直接导出
function MyConstructor (opts) {
//...
}
// BROKEN: Does not modify exports
exports = MyConstructor;
// exports the constructor properly
module.exports = MyConstructor;