Я определяю свои маршруты API в Nodejs, но при запуске он выдает это сообщение об ошибке.
Сообщение об ошибке
ReferenceError: Item не определено
at D:\shoppinglist\serverside\route\routes.js:7:5
at Layer.handle [as handle_request] (D:\shoppinglist\serverside\node_modules\express\lib\router\layer.js:95:5)
at next (D:\shoppinglist\serverside\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (D:\shoppinglist\serverside\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (D:\shoppinglist\serverside\node_modules\express\lib\router\layer.js:95:5)
at D:\shoppinglist\serverside\node_modules\express\lib\router\index.js:281:22
at Function.process_params (D:\shoppinglist\serverside\node_modules\express\lib\router\index.js:335:12)
at next (D:\shoppinglist\serverside\node_modules\express\lib\router\index.js:275:10)
at Function.handle (D:\shoppinglist\serverside\node_modules\express\lib\router\index.js:174:3)
at router (D:\shoppinglist\serverside\node_modules\express\lib\router\index.js:47:12)
at Layer.handle [as handle_request] (D:\shoppinglist\serverside\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (D:\shoppinglist\serverside\node_modules\express\lib\router\index.js:317:13)
at D:\shoppinglist\serverside\node_modules\express\lib\router\index.js:284:7
at Function.process_params (D:\shoppinglist\serverside\node_modules\express\lib\router\index.js:335:12)
at next (D:\shoppinglist\serverside\node_modules\express\lib\router\index.js:275:10)
at jsonParser (D:\shoppinglist\serverside\node_modules\body-parser\lib\types\json.js:110:7)
Мой маршрут. js код
var express = require('express');
var router = express.Router();
router.get('/items', (req, res, next)=>{
Item.find(function(err, items){
if(err){
res.json(err);
}
else{
res.json(items);
}
});
});
router.post('/item', (req, res, next)=>{
let newShoppingItem = new Item({
itemName: req.body.itemName,
itemQuantity: req.body.itemQuantity,
itemBought: req.body.itemBought
});
newShoppingItem.save((err, item)=>{
if(err){
res.json(err)
}
else{
res.json({msg: 'Item has been added to db'});
}
});
});
router.put('/put_test', (req, res, next)=>{
// do later
});
router.delete('/delete_test', (req, res, next)=>{
// do later
});
module.exports = router;
Вот мой shoopingitem. js код
shoppingitem. js
const mongoose = require('mongoose');
const shoppingItemSchema = mongoose.Schema({
itemName:{
type: String,
require:true
},
itemQuantity:{
type: Number,
require:true
},
itemBought:{
type: Boolean,
require:true
},
});
const Item = module.exports = mongoose.model('Item', shoppingItemSchema);
Пожалуйста, предложите мне решение. Я новичок в nodejs. Так что помогите пожалуйста
Заранее спасибо