未定义猫鼬模型

时间:2018-10-14 06:50:13

标签: javascript node.js express

使用猫鼬将我的应用程序链接到数据库时,我不断收到错误消息:

app.js(服务器):

const express = require('express');
const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/nodedb', { useNewUrlParser: true });
let db = mongoose.connection;

db.once('open', () => console.log('connected to MongoDB'));

db.on('error', (err) => console.log(err));

const app = express();

let Article = require('./models/article');

const path = require('path');

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

//create routes
app.get('/', (req, res) => {
    Acticle.find({}, (err, articles) => {
        if (err) {
            console.log(err)
        } else {
            res.render('index', {
                title: 'Some articles',
                articles: articles
            });
        }
    });
});

//start server at the port
app.listen(3000, () => console.log('server is up and listening at the port 3000'));

在上面的文件中,我连接到数据库,并且连接成功。

在app.js中,我引用了我的模型文件(article.js):

//bring in mongoose
const mongoose = require('mongoose');

//create an article schema
let articleSchema = mongoose.Schema({
    title: {
        type: String,
        required: true
    },
    author: {
        type: String,
        required: true
    },
    body: {
        type: String,
        required: true
    }
});

let Article = mongoose.model('Article', articleSchema);
module.exports = Article;

当我尝试访问“ /”路由时,出现错误消息

  

ReferenceError:未定义Acticle       在app.get(/ media / domanski / Domanski / 1_Tel Ran / webinars_2018 / 20181014 / nodebase_mongo / app.js:43:5)       在Layer.handle [作为handle_request](/ media / domanski / Domanski / 1_Tel   然/webinars_2018/20181014/nodebase_mongo/node_modules/express/lib/router/layer.js:95:5)       在下一个(/ media / domanski / Domanski / 1_Tel Ran / webinars_2018 / 20181014 / nodebase_mongo / node_modules / express / lib / router / route.js:137:13)       在Route.dispatch(/ media / domanski / Domanski / 1_Tel Ran / webinars_2018 / 20181014 / nodebase_mongo / node_modules / express / lib / router / route.js:112:3)       在Layer.handle [作为handle_request](/ media / domanski / Domanski / 1_Tel   然/webinars_2018/20181014/nodebase_mongo/node_modules/express/lib/router/layer.js:95:5)       在/ media / domanski / Domanski / 1_Tel Ran / webinars_2018 / 20181014 / nodebase_mongo / node_modules / express / lib / router / index.js:281:22       在Function.process_params(/ media / domanski / Domanski / 1_Tel Ran / webinars_2018 / 20181014 / nodebase_mongo / node_modules / express / lib / router / index.js:335:12)       在下一个(/ media / domanski / Domanski / 1_Tel Ran / webinars_2018 / 20181014 / nodebase_mongo / node_modules / express / lib / router / index.js:275:10)       在expressInit(/ media / domanski / Domanski / 1_Tel Ran / webinars_2018 / 20181014 / nodebase_mongo / node_modules / express / lib / middleware / init.js:40:5)       在Layer.handle [作为handle_request](/ media / domanski / Domanski / 1_Tel   然/webinars_2018/20181014/nodebase_mongo/node_modules/express/lib/router/layer.js:95:5)       在trim_prefix(/ media / domanski / Domanski / 1_Tel Ran / webinars_2018 / 20181014 / nodebase_mongo / node_modules / express / lib / router / index.js:317:13)       在/ media / domanski / Domanski / 1_Tel Ran / webinars_2018 / 20181014 / nodebase_mongo / node_modules / express / lib / router / index.js:284:7       在Function.process_params(/ media / domanski / Domanski / 1_Tel Ran / webinars_2018 / 20181014 / nodebase_mongo / node_modules / express / lib / router / index.js:335:12)       在下一个(/ media / domanski / Domanski / 1_Tel Ran / webinars_2018 / 20181014 / nodebase_mongo / node_modules / express / lib / router / index.js:275:10)       在查询时(/ media / domanski / Domanski / 1_Tel Ran / webinars_2018 / 20181014 / nodebase_mongo / node_modules / express / lib / middleware / query.js:45:5)       在Layer.handle [作为handle_request](/ media / domanski / Domanski / 1_Tel   ran / webinars_2018 / 20181014 / nodebase_mongo / node_modules / express / lib / router / layer.js:95:5)

有人知道我该怎么做吗?

2 个答案:

答案 0 :(得分:0)

您有拼写错误。

Acticle.find({}, (err, articles) => {});

应该

Article.find({}, (err, articles) => {});

答案 1 :(得分:0)

那里有两个问题,

  1. app.js中的错字
  

Acticle.find()应该是Article.find()

  1. 在article.js(模型)中,您应该使用 new 关键字定义新的架构。

    let articleSchema = mongoose.Schema({});

应替换为

let articleSchema = new mongoose.Schema({
    // your schema
});