Я пытаюсь создать простое приложение MERN
У меня есть файл server.js со схемой и одним маршрутом get
Когда я пытаюсь протестировать маршрут get с postmaster Iполучаю ошибку
Я уверен, что я делал это раньше с тем же кодом, и я использую только один маршрут, и я все еще получаю эту ошибку.
TypeError: Item.find is not a function
at router.get (/Users/user/Documents/_Work/cd/MERN/vs-mern-list/routes/api/items.js:7:10)
at Layer.handle [as handle_request] (/Users/user/Documents/_Work/cd/MERN/vs-mern-list/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/user/Documents/_Work/cd/MERN/vs-mern-list/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/user/Documents/_Work/cd/MERN/vs-mern-list/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/user/Documents/_Work/cd/MERN/vs-mern-list/node_modules/express/lib/router/layer.js:95:5)
at /Users/user/Documents/_Work/cd/MERN/vs-mern-list/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/user/Documents/_Work/cd/MERN/vs-mern-list/node_modules/express/lib/router/index.js:335:12)
at next (/Users/user/Documents/_Work/cd/MERN/vs-mern-list/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/Users/user/Documents/_Work/cd/MERN/vs-mern-list/node_modules/express/lib/router/index.js:174:3)
at router (/Users/user/Documents/_Work/cd/MERN/vs-mern-list/node_modules/express/lib/router/index.js:47:12)
at Layer.handle [as handle_request] (/Users/user/Documents/_Work/cd/MERN/vs-mern-list/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/user/Documents/_Work/cd/MERN/vs-mern-list/node_modules/express/lib/router/index.js:317:13)
at /Users/user/Documents/_Work/cd/MERN/vs-mern-list/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/Users/user/Documents/_Work/cd/MERN/vs-mern-list/node_modules/express/lib/router/index.js:335:12)
at next (/Users/user/Documents/_Work/cd/MERN/vs-mern-list/node_modules/express/lib/router/index.js:275:10)
at /Users/user/Documents/_Work/cd/MERN/vs-mern-list/node_modules/body-parser/lib/read.js:130:5
Мои файлы:
server.js
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const items = require('./routes/api/items');
const app = express();
app.use(bodyParser.json());
const db = require('./config/keys').mongoURI;
mongoose
.connect(db ,{ useNewUrlParser: true })
.then(() => console.log('db connected'))
.catch(err => console.log(err));
app.use('/api/items', items);
const port = process.env.PORT || 5000;
app.listen(port, () => console.log('server connected'));
Item.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const ItemSchema = new Schema({
name:{
type: 'String',
required: true
},
Date:{
type: Date,
default: Date.now
}
})
module.export = Item = mongoose.model('item', ItemSchema);
items.js
const express = require('express');
const router = express.Router();
const Item = require('../../models/Item');
router.get('/', (req, res) => {
Item.find()
.then(items => res.json(items))
.catch(err => console.log(err))
});
module.exports = router;