Mongoose is returning an empty array from my database when doing a Model.find() query










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!`));









share|improve this question






















  • 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















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!`));









share|improve this question






















  • 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













0












0








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!`));









share|improve this question














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!`));






mongodb express mongoose






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 18:44









CodenamiCodenami

198




198












  • 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

















  • 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
















Try passing an empty query object: Char.find(, function(err, chars) ... )

– Steve Holgado
Nov 14 '18 at 19:08





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






@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





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





@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





And you definitely have records in the database for that collection?

– Steve Holgado
Nov 14 '18 at 23:03












1 Answer
1






active

oldest

votes


















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");





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306847%2fmongoose-is-returning-an-empty-array-from-my-database-when-doing-a-model-find%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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");





    share|improve this answer



























      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");





      share|improve this answer

























        0












        0








        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");





        share|improve this answer













        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");






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 19:05









        CodenamiCodenami

        198




        198





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306847%2fmongoose-is-returning-an-empty-array-from-my-database-when-doing-a-model-find%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            ReactJS Fetched API data displays live - need Data displayed static

            Evgeni Malkin