0

I looked at this popular question, but it didn't seem to fix my issue, so I'm going to post this.

I currently have an express.js server file using mongoose, that keeps returning an empty array. I have no idea if it might by an async issue, and I don't know what I can use to indicate that I'm connected to my database.

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const PORT = process.env.PORT || 8080;


//Mongoose stuff
mongoose.connect('mongodb+srv://excelsiorAdmin:Mysecretpassword@excelsiorcluster-zakfd.mongodb.net/test?retryWrites=true', { useNewUrlParser: true, dbName: 'excelsiorDB'});
const dbConnection = mongoose.connection;

dbConnection.on('error', console.error.bind(console, 'connection error:'));
dbConnection.once('open', function() {
    console.log('connected to the database');

    let charSchema = new mongoose.Schema({
        imageURL: String,
        company: String,
        name: String,
        civName: String,
        alignment: String,
        firstDebut: String,
        abilities: Array,
        teams: Array,
        desc: String
    });

    let Char = mongoose.model('Char', charSchema, 'chars');

    //root
    app.get('/', (req, res, next) => res.send('Welcome to the API!'));

    //get all characters
    app.get('/chars', (req, res, next) => {
        console.log('getting all characters');
        Char.find(function (err, chars) {
            if (err) {
                res.status(404).send(err);
                console.log('there was an error');
              };
              console.log(chars);
              res.send(chars);
        });
    });

    //get heroes
    app.get('/chars/heroes', (req, res, next) => {
        Char.find({alignment: "Hero"}, function (err, chars) {
            if (err) {
                res.status(404).send(err);
            };
            res.send(chars);
        });
    });

});

app.listen(PORT, () => console.log(`This API is listening on port ${PORT}!`));
Codenami
  • 31
  • 1
  • 10
  • Try passing an empty query object: `Char.find({}, function(err, chars) { ... })` – Steve Holgado Nov 14 '18 at 19:08
  • @SteveHolgado I changed the find all query to this: `app.get('/chars', (req, res, next) => { console.log('getting all characters'); Char.find({}, function (err, chars) { if (err) { res.status(404).send(err); console.log('there was an error'); }; console.log(chars); res.send(chars); }); });` It still returns an empty array. would this mean that mongoose is not properly connected to the database? – Codenami Nov 14 '18 at 19:16
  • Do you see 'connected to the database' logged to the console? – Steve Holgado Nov 14 '18 at 19:53
  • @SteveHolgado Yes, which should mean that Mongoose itself is working. The console.log for the Model.find returns the char object as an empty array. – Codenami Nov 14 '18 at 21:11
  • And you definitely have records in the database for that collection? – Steve Holgado Nov 14 '18 at 23:03
  • @SteveHolgado Yes, the database consists of one collection with 57 documents. – Codenami Nov 15 '18 at 00:22
  • Does your `/chars/heroes` route find documents correctly? ...or does that also return empty? – Steve Holgado Nov 15 '18 at 08:56

1 Answers1

0

The mongoose.model will set the collection it's looking for equal to the lowercase, pluralized form of the name of the model.

let Char = mongoose.model('Char', charSchema);

This will look for the "chars" collection. However, if the database you're connecting to doesn't have a collection with the same name as the mongoose default, it will return results from a collection that doesn't exist. To make sure it hits the right collection if they don't match, you'll have to manually enter the collection's name as a third parameter:

let Char = mongoose.model('Char', charSchema, "excelsiorCollection");
Codenami
  • 31
  • 1
  • 10