0

i created a mongodb with a name userInfo with collection name "info" in the terminal and i created a schema in models/user_info.js.And i am not getting any values from the database at all. my userInfo db has name,studentId,dept

My view engine is jade/pug I am trying to iterate and display the values from the database. I am not getting an error but its not displaying the values. Thanks for the help!

app.js

const express= require('express');
const path = require('path')
const mongoose = require('mongoose')
const bodyParser = require('body-parser')

mongoose.connect('mongodb://localhost/userInfo')
let db = mongoose.connection;


db.on('error',(error) => console.log(error));
db.once('open',() => console.log('Connected to Mongodb'))

const app = express()

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())


let UserInfo = require('./models/user_info')

app.set('views',path.join(__dirname,'views'))
app.set('view engine','pug')

app.get('/',(req,res) => {
    UserInfo.find({},function(err,info){
        if(err){
            console.log(err);
        }else{
            res.render('tabel',{
                title:'user report',
                info:info
            });
        }
    })
})

user_info.js //shema

let mongoose = require('mongoose');


//Userinfo Schema
let userInfoSchema = mongoose.Schema({
    name:{
        type:String,
        required:true
    },
    studentID:{
        type:String,
        required:true
    },
    dept:{
        type:String,
        required:true
    }
});

let UserInfo = module.exports = mongoose.model('UserInfo',userInfoSchema);
e.T55
  • 443
  • 1
  • 4
  • 21
  • 1
    Just to verify, have you tried logging the info before you call `res.render()`? What does it print? – ajbieber Feb 19 '19 at 19:17
  • it prints empty list – e.T55 Feb 19 '19 at 19:34
  • Ok, another thing to try is remove the `let UserInfo =` from before your module.exports in user_info.js, that does not need to be there. – ajbieber Feb 19 '19 at 19:37
  • Still returning empty list – e.T55 Feb 19 '19 at 19:45
  • And you are sure you have data in the database? Just trying to cover everything here... Can you post that data? – ajbieber Feb 19 '19 at 19:51
  • `use userInfo` `switched to db userInfo` `db.info.find();` "_id" : ObjectId("5c6c4913a680407b7a4b800f"), "name" : "ezana tesfaye", "studentID" : "013744783", "dept" : "CMPE" }` – e.T55 Feb 19 '19 at 19:57
  • 1
    Possible duplicate of [Mongoose -- Force collection name](https://stackoverflow.com/questions/7486528/mongoose-force-collection-name) That explains how to associate a collection with your model. – Robert Moskal Feb 19 '19 at 20:00
  • You can get data from `info` collection, but In your application, you are trying to get data from `UserInfo` collection. – Shams Nahid Feb 20 '19 at 04:37

1 Answers1

1

In your model, you are pointing the collection name UserInfo, but your data are in 'info' collection.

So, change the collection name explicitly in your model.

Your user_info.js (schema) should be,

let mongoose = require('mongoose');
let userInfoSchema = mongoose.Schema({
    name:{
        type:String,
        required:true
    },
    studentID:{
        type:String,
        required:true
    },
    dept:{
        type:String,
        required:true
    }
});

let UserInfo = module.exports = mongoose.model('UserInfo', userInfoSchema, 'info');

In the last line, I pass the third argument, to indicate the collection name to info.

Shams Nahid
  • 6,239
  • 8
  • 28
  • 39