Express JS-查找不是函数

时间:2019-02-25 06:52:43

标签: express mern

我正在尝试创建一个简单的MERN应用

我有一个带模式的server.js文件和一个获取路由

当我尝试与邮局局长测试获取路线时,会收到错误消息

我确定我之前已经用相同的代码完成了此操作,并且只使用了一条路由,但仍然收到此错误。

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;    

1 个答案:

答案 0 :(得分:0)

您的模块导出错误

明确地说,不应有这样的出口

module.export = Item = mongoose.model('Item', ItemSchema);  
//or
mongoose.exports = mongoose.model('Item', listSchema);

应该是

module.exports = Item = mongoose.model('Item', ItemSchema); 
//or
module.exports = mongoose.model('Item', ItemSchema);