0

Please find the code of app.js var express = require('express'), routes = require('./routes');

var mongoose = require('mongoose');

var app = module.exports = express.createServer();

// Configuration

app.configure(function(){

  app.set('views', __dirname + '/views');

  app.set('view engine', 'jade');

  app.use(express.bodyParser());

  app.use(express.methodOverride());

  app.use(app.router);

  app.use(express.static(__dirname + '/public'));   });

app.configure('development', function(){

  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));

});

app.configure('production', function(){

  app.use(express.errorHandler());  });

// Routes

app.get('/', routes.index);

app.listen(3000, function(){

  console.log("Express server listening on port %d in %s mode", 

 app.address().port, app.settings.env); });


mongoose.connect('mongodb://localhost/mydb', function (error) {

    if (error) {

    console.log(error);

    }

});

var Schema = mongoose.Schema;

var UserSchema = new Schema({

    first_name: String,

    last_name: String,

    email: String

});

// Mongoose Model definition

var User = mongoose.model('users', UserSchema);


app.get('/', function (req, res) {

    res.send("<a href='/users'>Show Users</a>");

});

app.get('/users', function (req, res) {

    User.find({}, function (err, docs) {

    res.json(docs);

    });

});

app.get('/users/:email', function (req, res) {

    if (req.params.email) {

    User.find({ email: req.params.email }, function (err, docs) {

        res.json(docs);

    });

    }

});

Mongodb is running and in mydb and inside collection below document is present { "_id" : ObjectId("562101187941ab21a444c286"), "first_name" : "u1", "last_name"

: "u1", "email" : "an@ii.com" }

But when i run nodejs app.js and connect to http://localhost:3000/users it shows [] and not the data in database. Please help

General Grievance
  • 4,555
  • 31
  • 31
  • 45
joy
  • 9
  • 5

1 Answers1

1

Actually, your collection should be named "users" for mongoose to find it. Mongoose normally save collections with plural names, adding "s" to model names. Since your model name is already plural it should be named "users".

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Anton F
  • 328
  • 1
  • 8
  • My collection name is users only in db, but also not getting the data on web page – joy Oct 16 '15 at 15:23