简体   繁体   English

未定义猫鼬模型

[英]Mongoose Model is not defined

Linking my app to database utilizing mongoose I constantly get an error: 使用猫鼬将我的应用程序链接到数据库时,不断出现错误:

app.js (server): 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'));

In the file above I connect to the db, and the connection is successful. 在上面的文件中,我连接到数据库,连接成功。

In app.js I reference my model file (article.js): 在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;

and when I try to access the '/' route, I get an error of 当我尝试访问“ /”路由时,出现错误

ReferenceError: Acticle is not defined at app.get (/media/domanski/Domanski/1_Tel Ran/webinars_2018/20181014/nodebase_mongo/app.js:43:5) at Layer.handle [as handle_request] (/media/domanski/Domanski/1_Tel Ran/webinars_2018/20181014/nodebase_mongo/node_modules/express/lib/router/layer.js:95:5) at next (/media/domanski/Domanski/1_Tel Ran/webinars_2018/20181014/nodebase_mongo/node_modules/express/lib/router/route.js:137:13) at Route.dispatch (/media/domanski/Domanski/1_Tel Ran/webinars_2018/20181014/nodebase_mongo/node_modules/express/lib/router/route.js:112:3) at Layer.handle [as handle_request] (/media/domanski/Domanski/1_Tel Ran/webinars_2018/20181014/nodebase_mongo/node_modules/express/lib/router/layer.js:95:5) at /media/domanski/Domanski/1_Tel Ran/webinars_2018/20181014/nodebase_mongo/node_modules/express/lib/router/index.js:281:22 at Function.process_params (/media/domanski/Domanski/1_Tel Ran/webinars_2018/20181014/nodebase_mongo/node_modules/express/lib/router/index.js:335:12) at n ReferenceError:Acticle未在Layer.handle的app.get(/ media / domanski / Domanski / 1_Tel Ran / webinars_2018 / 20181014 / nodebase_mongo / app.js:43:5)中定义[作为handle_request](/ media / domanski / Domanski / 1_Tel Ran / 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)位于Layer的Route.dispatch(/ media / domanski / Domanski / 1_Tel Ran / webinars_2018 / 20181014 / nodebase_mongo / node_modules / express / lib / router / route.js:112:3) .handle [作为handle_request](/ media / domanski / Domanski / 1_Tel Ran / 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 at Function.process_params(/ media / domanski / Domanski / 1_Tel Ran / webinars_2018 / 20181014 / nodebase_mongo / node_modules / express / lib / router / index.js:335:12)在n ext (/media/domanski/Domanski/1_Tel Ran/webinars_2018/20181014/nodebase_mongo/node_modules/express/lib/router/index.js:275:10) at expressInit (/media/domanski/Domanski/1_Tel Ran/webinars_2018/20181014/nodebase_mongo/node_modules/express/lib/middleware/init.js:40:5) at Layer.handle [as handle_request] (/media/domanski/Domanski/1_Tel Ran/webinars_2018/20181014/nodebase_mongo/node_modules/express/lib/router/layer.js:95:5) at trim_prefix (/media/domanski/Domanski/1_Tel Ran/webinars_2018/20181014/nodebase_mongo/node_modules/express/lib/router/index.js:317:13) at /media/domanski/Domanski/1_Tel Ran/webinars_2018/20181014/nodebase_mongo/node_modules/express/lib/router/index.js:284:7 at Function.process_params (/media/domanski/Domanski/1_Tel Ran/webinars_2018/20181014/nodebase_mongo/node_modules/express/lib/router/index.js:335:12) at next (/media/domanski/Domanski/1_Tel Ran/webinars_2018/20181014/nodebase_mongo/node_modules/express/lib/router/index.js:275:10) at query (/media/domanski/Domanski/ ext(/ 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 Ran / webinars_2018 / 20181014 / nodebase_mongo / node_modules / express / lib /在/ media / domanski的trim_prefix(/ media / domanski / Domanski / 1_Tel Ran / webinars_2018 / 20181014 / nodebase_mongo / node_modules / express / lib / router / index.js:317:13)处的router / layer.js:95:5) / 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 /在下一个(/ media / domanski / Domanski / 1_Tel Ran / webinars_2018 / 20181014 / nodebase_mongo / node_modules / express / lib / router / index.js:275:10)的express / lib / router / index.js:335:12)查询(/ media / domanski / Domanski / 1_Tel Ran/webinars_2018/20181014/nodebase_mongo/node_modules/express/lib/middleware/query.js:45:5) at Layer.handle [as handle_request] (/media/domanski/Domanski/1_Tel Ran/webinars_2018/20181014/nodebase_mongo/node_modules/express/lib/router/layer.js:95:5) 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 /快递/ LIB /路由器/ layer.js:95:5)

Does anyone has an Idea what do I do wrong? 有人知道我该怎么做吗?

You have spelling mistake. 您有拼写错误。

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

Should be 应该

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

Two issues there, 那里有两个问题,

  1. Typo in app.js app.js中的错字

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

  1. In article.js(model), you should use the new keyword to define a new schema. 在article.js(model)中,应该使用new关键字定义新的架构。

    let articleSchema = mongoose.Schema({}); 让articleSchema = mongoose.Schema({});

should be replaced with 应该替换为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM