无法在Node.Js中的其他文件中调用Mongoose函数

问题描述 投票:0回答:1

我创建了一些在一个文件中包含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
node.js mongodb mongoose
1个回答
0
投票

检查链模型,以确保您要导出countDocuments函数,并检查拼写是否已导出

© www.soinside.com 2019 - 2024. All rights reserved.