0

This is model in models.js

var PatientSchema = new mongoose.Schema({
     _id : String,
     LastName : String,
     MiddleName : String,
     PatientIntId : String,
     Sex : String,
     Address1 : String,
     City : String,
     State : String,
     ZipCode : String,
     AccountNumber : String,
     Ssn : String
 });

var PatientInfoMdl = mongoose.model('PatientInfo',PatientSchema);
exports.PatientInfoMdl = PatientInfoMdl;

and my code for accessing data is :

var dbObj = require('../dataBase');
var config = require('../config');<
var moment = require('moment');
var models = require('../models/models');
var orm = require('orm');
var xml2js = require('xml2js');
var fs = require('fs');
var user = models.user;
var PatientInfoMdl = models.PatientInfoMdl;
exports.DisplayUsers = function (req, res) {
    var name = '';
    dbObj.connect(config.get('mongo'), function () {
        PatientInfoMdl.find()({}, function (err, docs) {
            if (err) 
                res.json(err);
            else res.render('index', { Patients : docs }); 
        });
    });
}

and I am not getting data and what is my mistake?

fernandopasik
  • 9,565
  • 7
  • 48
  • 55

2 Answers2

0

My mistake is not following the naming conventions of collections in mongoDB.

Is there a convention to name collection in MongoDB?

Community
  • 1
  • 1
0

For example:

Controller.js

var mongoose = require('mongoose');


var User = mongoose.model('User');

module.exports = {

  show: function(req, res) {

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

      res.render('main', {users: users});

    })
  }
}

Models:User.js

// require mongoose
var mongoose = require('mongoose');

// create the UserSchema

var UserSchema = new mongoose.Schema({
  name: String
})

// register the schema as a model

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

module.exports = {User}

routes.js

// here we load the Quote model that we created on the server.js page

var mongoose = require('mongoose');

var User = mongoose.model('User');

// import users

var users = require('../controllers/users.js');

module.exports = function(app) {

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

    res.send("Hello");

  })

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

      users.show(req,res);

  })
}
Fares M.
  • 1,538
  • 1
  • 17
  • 18
ASHISH R
  • 4,043
  • 1
  • 20
  • 16