javascript - Mongoose ,出现类型错误 : Cannot read properties of undefined (reading 'find' )

标签 javascript node.js mongodb express mongoose

我收到错误,TypeError:无法读取未定义的属性(读取“find”),它指向代码块:

app.get('/Organizations', (req,res) => {
Organizations.find({}).then((organization) => {
    res.send(organization);
}); })

app.js,导入 Mongoose 架构:

const {Organizations} = require('./db/models');

组织.model.js:

const mongoose = require('mongoose');

const OrganizationsSchema = new mongoose.Schema({
    organizationName:{
        type: String,
        required: true,
        minlength:1,
        trim: true
    }
})

    
const Organizations = mongoose.model( 'Organizations', OrganizationsSchema);

module.exports =  (Organizations)

index.js:

const { Organizations } = require('./Organizations.model');
module.exports = {
    Organizations
}

最佳答案

这是一个导入/导出问题。在 Organization.model.js 中,导出 Organizations 时使用括号而不是大括号。像这样导出:

module.exports =  {Organizations}

并以这种方式导入:

const { Organizations } = require('./Organizations.model');

关于javascript - Mongoose ,出现类型错误 : Cannot read properties of undefined (reading 'find' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70505335/

相关文章:

javascript - 当 rxjs.race() 获胜时,其他可观察量会发生什么?

node.js - 您将 $NVM_DIR 设置为 "/root/.nvm",但该目录不存在。检查您的配置文件和环境

javascript - 在没有自动调整的情况下将我的图像 slider 定位在屏幕上

Javascript 滚动到中心或滚动到正文

node.js - twilio 操作 url 中的查询参数

javascript - 如何在 meteor 应用程序上使用 X 射线?

node.js - mongodb 并在 node.js 中进行身份验证和通行证

javascript - Mongoose - 从 'pre' 保存 Hook 中的虚拟节点访问数据

node.js - Mongoose:查找、更新、保存 - 没有错误,保存回调也发生变化,但在 MongoDB 中没有

javascript - 如何根据单元格单击突出显示表格行?