【发布时间】: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