【问题标题】:Mongoose, getting TypeError: Cannot read properties of undefined (reading 'find')获取 TypeError:使用 Mongoose 时无法读取未定义的属性(读取“查找”)
【发布时间】:2022-01-27 00:00:16
【问题描述】:

我收到错误,TypeError: Cannot read properties of undefined (reading 'find') 指向代码块:

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

app.js,导入猫鼬模式:

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

Organization.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
}

【问题讨论】:

  • 你能说明你在哪里导出和导入Organizations 吗?

标签: javascript node.js mongodb express mongoose


【解决方案1】:

您在导出 Organisations 时添加了圆括号而不是方括号。试试这个:

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}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 2021-12-21
    • 1970-01-01
    • 2021-11-26
    相关资源
    最近更新 更多