Monday 1 May 2017

Express/Node.js error: Reference Error: Res is not defined

I'm trying to create a basic CRUD application right now for my group's application but I'm having some trouble with a basic line. To build my crud application, I'm currently using node.js, express, and mongodb. I'm having some trouble with part of my code and I was wondering if you could point me toward the right direction.

(this is a lengthy question. i tried my best to format this so that it wouldn't burn your eyes while reading... sorry if it is hard to read)

context: for reference I'm using this:

https://zellwk.com/blog/crud-express-mongodb/, currently on "Showing quotes to users" 

my problem: every time i initialize res.render(view, locals) my node server crashes. here's the error that i'm getting.

res.render(view, locals)
    ReferenceError: res is not defined
        at Object.<anonymous> (C:\Users\hcqph\xx\xcxx\server.js:35:1)
        at Module._compile (module.js:570:32)
        at Object.Module._extensions..js (module.js:579:10)
        at Module.load (module.js:487:32)
        at tryModuleLoad (module.js:446:12)
        at Function.Module._load (module.js:438:3)
        at Module.runMain (module.js:604:10)
        at run (bootstrap_node.js:393:7)
        at startup (bootstrap_node.js:150:9)
        at bootstrap_node.js:508:3

my code: this is what i have for server.js so far. right now, i'm getting an error whenever i try to import my index.ejs file and whenever i try to import the following lines:

res.render(views, locals)

this is my code for server.js

    const express = require('express');
    const bodyParser = require('body-parser')
    const app = express();
    const MongoClient = require('mongodb').MongoClient

  var db

    MongoClient.connect('mongodb://omittedforprivacy', (err, database) => {
        if (err) return console.log(err)
        db = database

        app.listen(3000, () => {
            console.log('connected to mongoDB successfully. now listening on port 3000 ')
        })

    })

    /* -------- for team documentation --------
    BODYPARSER makes it possible to handle reading data from form element in index.html.  URLENCODED method within body-parser tells body-parser to extract data from form element and add them to the body element property in request object.
    ------------------------------------------------ */ 
    app.use(bodyParser.urlencoded({extended: true}))

    //set 'ejs' template engine, and default extension is ejs
    app.set('view engine', 'ejs')

    //CAN'T FIGURE OUT WHY THIS ISN'T WORKING
    res.render(views, locals)

    /* -------- for team documentation --------
    => = replacement for function

    app.get hand a GET request (read operation)
    ------------------------------------------------ */
    app.get('/', (req, res) => {
        //serves index.html back to browser
        res.sendFile(__dirname + '/index.html')

        //gets list of trips from mlab.com
        var cursor = db.collection('trips').find()

        //retrieves list of trips retrieved from mlab
        db.collection('trips').find().toArray(function(err, results) {
            if(err) return console.log(err)

            //renders index.ejs
            res.render('index.ejs', {trips:results})
        })
    })



    /* app.post handles a create request */
    app.post('/trips', (req, res) => {
        db.collection('trips').save(req.body, (err, result) =>{
            if (err) return console.log(err)

            console.log('saved to database')
            res.redirect('/') //causes browser to reload
        })
    })

Any assistance that you all could provide to help me troubleshoot would be really really appreciated. I've been stuck on this for the past few hours. I thought res was already defined by express? Why am I being told to define res again?

edit: omitted some stuff for privacy reasons



via Clark

No comments:

Post a Comment