0

Linking my app to database utilizing mongoose I constantly get an error:

app.js (server):

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):

//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 next (/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/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)

Does anyone has an Idea what do I do wrong?

3
  • 1
    You have Acticle spelling mistake. Commented Oct 14, 2018 at 6:56
  • 1
    flagging for closure because typo Commented Oct 14, 2018 at 7:22
  • yes, guys, thanks, my bad Commented Oct 14, 2018 at 11:36

2 Answers 2

1

Two issues there,

  1. Typo in app.js

Acticle.find() should be Article.find()

  1. In article.js(model), you should use the new keyword to define a new schema.

    let articleSchema = mongoose.Schema({});

should be replaced with

let articleSchema = new mongoose.Schema({ // your schema }); 
Sign up to request clarification or add additional context in comments.

Comments

0

You have spelling mistake.

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

Should be

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.