2

Making a voting app that consists of following 2 models users and polls and database consisting of two collections users and polls.

User.js

'use strict';

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var User = new Schema({
    github: {
        id: String,
        displayName: String,
        username: String,
        publicRepos: Number
    },
    nbrClicks: {
        clicks: Number
    }
});
module.exports = mongoose.model('User', User);

Poll.js

'use strict';

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Poll = new mongoose.Schema({
    title: { type: String, required: true },
    creator: String,
    choices: [String],
    votes: [Number]
});
module.exports = mongoose.model('Poll', Poll);

and this is the route part

'use strict';
require('dotenv').load();
var path = process.cwd();
var ClickHandler = require(path + '/app/controllers/clickHandler.server.js');
var mongo = require('mongodb');
var mongoose = require('mongoose');
var db = mongoose.createConnection(process.env.MONGO_URI);

module.exports = function (app, passport) {
    function isLoggedIn (req, res, next) {
        if (req.isAuthenticated()) {
            return next();
        } else {
            res.redirect('/login');
        }
    }

    var clickHandler = new ClickHandler();
    app.route('/').get(isLoggedIn, function (req, res) {
        //console.log(display-name.profile-value);
        res.render(path + '/public/index.jade');
    });
    app.route('/polls').get(function(req, res) { 
        var polls = db.get('polls');
        console.log(polls);
        res.render(path + '/public/polls.jade');
    })

This is the error being thrown

TypeError: db.get is not a function
at /home/ubuntu/workspace/app/routes/index.js:39:22
at Layer.handle [as handle_request] (/home/ubuntu/workspace/node_modules/express/lib/router/layer.js:95:5)
at next (/home/ubuntu/workspace/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/home/ubuntu/workspace/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/ubuntu/workspace/node_modules/express/lib/router/layer.js:95:5)
at /home/ubuntu/workspace/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:330:12)
at next (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:271:10)
at SessionStrategy.strategy.pass (/home/ubuntu/workspace/node_modules/passport/lib/middleware/authenticate.js:325:9)
at SessionStrategy.authenticate (/home/ubuntu/workspace/node_modules/passport/lib/strategies/session.js:71:10)
at attempt (/home/ubuntu/workspace/node_modules/passport/lib/middleware/authenticate.js:348:16)
at authenticate (/home/ubuntu/workspace/node_modules/passport/lib/middleware/authenticate.js:349:7)
at Layer.handle [as handle_request] (/home/ubuntu/workspace/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:312:13)
at /home/ubuntu/workspace/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:330:12)
at next (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:271:10)
at initialize (/home/ubuntu/workspace/node_modules/passport/lib/middleware/initialize.js:53:5)
at Layer.handle [as handle_request] (/home/ubuntu/workspace/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:312:13)
at /home/ubuntu/workspace/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:330:12)
alex
  • 5,467
  • 4
  • 33
  • 43
Diwakar Moturu
  • 652
  • 5
  • 13
  • I'm gonna guess the problem is that `db.get` isn't a function. You're going to want to do something with `Poll.find` [the mongoose docs](http://mongoosejs.com/docs/) – numbers1311407 Aug 01 '16 at 14:18

2 Answers2

2

First you need to import Poll into your route file.

var Poll = require('./PATH_TO_POLL/Poll');

Then as @numbers1311407 has described, if you want to return all the saved Poll documents try:

Poll.find(function (err, polls) {
  if (err) return console.error(err);
  console.log(polls);
})

edit:

More information on module.exports can be found here

Community
  • 1
  • 1
alex
  • 5,467
  • 4
  • 33
  • 43
  • but, actually I have exported the Poll model using module.exports, so cant it be called simply without using require method elsewhere in the directory..? – Diwakar Moturu Aug 02 '16 at 02:42
  • Your welcome, happy to help. You have to require `'Poll'`, what you call it here `module.exports = mongoose.model('Poll', Poll);` indicates by convention you should call it with the same name, but you could call it anything. Stick to the naming convention though, it makes your code easier to read. – alex Aug 02 '16 at 02:47
-1

this error is comes when using mongodb community server. so you need to use mongoose or another one.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 10 '22 at 05:35