我在Nodejs中定义了我的API路由,但是当我运行它时,它会给出此错误消息。
错误消息
ReferenceError:未定义项目
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)
我的Route.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的新手。所以请帮助
预先感谢
答案 0 :(得分:0)
您需要在路由文件中需要Item模型才能使用它。这可以通过两种方式完成:
const Item = require('../path/to/item/shoppingItem.js')
或
const mongoose = require('mongoose')
const Item = mongoose.model('Item');
答案 1 :(得分:0)
正如Matthew Weeks所提到的,请确保将猫鼬Item模型导入到Route.js文件中。
此外,如果您要查询集合中的所有项目,请确保在查找功能中包括一个空的查询对象作为第一个参数。包括Matthew Weeks解决方案在内,Route.js的第一部分应类似于:
const Item = require('../path/to/item/shoppingItem.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);
}
});