【问题标题】:Cannot read property 'find' of undefined while Finding Data查找数据时无法读取未定义的属性“查找”
【发布时间】:2021-08-11 20:54:57
【问题描述】:

遇到错误!

我不知道该怎么办,请帮我解决错误

错误:- 错误表明 find 未定义

{"message":"Cannot read property 'find' of undefined"}

post.js

控制器

const mongoose = require('mongoose')

const { Restaurant } = require('../models/restaurantsSchema')

const getPosts = (req,res) => {
    res.status(200).send('Hi, their')
}

const api = async (req,res) => {
    try {
        const sample_restaurants = await Restaurant.find()

        res.status(200).json(sample_restaurants)

    } catch (error) {
        res.status(404).json({ message: error.message })
    }
}

module.exports = {
    getPosts,
    api,
}

架构

我在这里创建了架构

const mongoose = require('mongoose')

const restaurantsSchema = mongoose.Schema({
    address : Object,
    grades : Array,
    name : String,
    borough : String,
    cuisine : String,
    restaurant_id : String,
})

var Restaurant = mongoose.model('Restaurant', restaurantsSchema);

module.exports = Restaurant;

【问题讨论】:

  • 请同时指出错误。
  • Restaurant 未定义。可以在问题中添加restaurantsSchema的内容吗?

标签: node.js json mongodb mongoose schema


【解决方案1】:
  • 我注意到,如果在 module.exports 中只有两个元素,那么为什么要在 api 之后放置逗号,即
module.exports = {
  getPosts,
  api
}

这可能对你有帮助。

【讨论】:

    猜你喜欢
    • 2021-12-21
    • 2020-10-04
    • 2018-12-29
    • 2015-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多