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);
8
  • 1
    Just to verify, have you tried logging the info before you call res.render()? What does it print? Commented Feb 19, 2019 at 19:17
  • it prints empty list Commented Feb 19, 2019 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. Commented Feb 19, 2019 at 19:37
  • Still returning empty list Commented Feb 19, 2019 at 19:45
  • 1
    Possible duplicate of Mongoose -- Force collection name That explains how to associate a collection with your model. Commented Feb 19, 2019 at 20:00

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.