1

I am simply trying to retrieve all the document from the database but I am getting empty array. I am using express, mongodb and mongoose. Below is my code :

const express = require('express');
const countryArray = require('../models/countryArray');
const router = express.Router();

router.get('/',(req,res)=>{
      countryArray.find({ },function (err, result) {
      if (err) 
        return console.error(err);
      console.log(result);
    });
    res.render('index');
});
module.exports = router ;

My Schema is this:

const mongoose = require('mongoose');
var Schema = mongoose.Schema;
var countrySchema = new Schema({
        country_id:String,
        country_name:String
});
var countryArray = mongoose.model('countryArray',countrySchema);
module.exports = countryArray;

Below is my document in mongodb:

{
    "_id": {
        "$oid": "5bba2693e7179a6602f63589"
    },
    "country_id": "1",
    "country_name": "India"
}
surendra
  • 41
  • 1
  • 4

1 Answers1

-1

I'm not completely sure, but I think the following may be related/helpful: Naming your model.

Afaik it goes beyond being just a "good practice", but I think the model name (countryArray) should be in PascalCase. (CountryArray, instead of countryArray)

var countryArray = mongoose.model('countryArray',countrySchema);

Hope this helps.

saglamcem
  • 677
  • 1
  • 8
  • 15