我创建了一些在一个文件中包含MongoDB方法的函数。当我从同一文件访问该功能时,它很好用,但是当我尝试从另一个文件访问该功能时,它不起作用。
这里是代码
const Chain = require('../database/models/chains')
const getlatestChain = async () => {
try {
const thechains = await Chain.countDocuments()
if (thechains < 2) {
throw new Error('there is only one chain!')
}
return thechains
} catch (error) {
return error
}
}
module.exports = {
getlatestChain: getlatestChain
}
当我从另一个文件中调用它时不起作用
const thechain = require('../src/utils/chain')
require('../src/database/database')
thechain.getlatestChain()
.then((result) => {
console.log('how many documents : ' + result)
}).catch((err) => {
console.log(err)
});
错误
TypeError: Chain.countDocuments is not a function
检查链模型,以确保您要导出countDocuments函数,并检查拼写是否已导出