0

This is my GET request

var Catalog = mongoose.model('Catalog');
router.get('/catalog', function(req, res, next) {
  Catalog.find(function(err, items){
    if(err){
      return next(err);
    }
    console.log(items);
    res.json(items);
  });
});

The model

var mongoose = require('mongoose');

var CatalogSchema = new mongoose.Schema({
    title: String,
    cost: String,
    description: String
});

mongoose.model('Catalog', CatalogSchema);

The console.log gives me a [] but there is a collection named Catalog with the parameters filled.

0

2 Answers 2

2

If you have an existing collection, created outside of Mongoose, that you wish to query you need to configure your model to use that collection.

Otherwise, Mongoose will use a utility function to create a collection name by pluralizing and lowercasing the model name (which in your case would become catalogs).

To use Catalog as a collection, use the collection option:

var CatalogSchema = new mongoose.Schema({
  title: String,
  cost: String,
  description: String
}, { collection : 'Catalog' });
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, you were right. I created the collection outside Mongoose and now I can query it!
0
  1. you might check again the collection name in mongodb i.e whether it is 'Catalog' or 'Catalogs'. In my experience I found that if you name the model 'Catalog', then either mongoose creates the collection with a name 'Catalogs' if it is not already there or you have to create the collection first with a name 'Catalogs'.

  2. You might check the following line also:

    Catalog.find({}, function(err, items){ // your code goes here }

Hope it helps..

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.