0

I am writing code with node.js. Quite new to this and the problem is that mongoose returns an empty array. There must be a mistake somewhere in this code, but I cannot find it. Any ideas?

Dresses schema

var dressesSchema = mongoose.Schema({
    title:{
    type: String,
    required: true
},
description:{
    type: String,
    required: true
}
});

var Dress = module.exports = mongoose.model('Dress', dressesSchema);

Get dresses from database

module.exports.getDresses = function(callback, limit){
    Dress.find(callback).limit(limit);
};

Dress = require('./models/dress');

app.get('/api/dresses', function(req, res){
    Dress.getDresses(function(err, dresses){
        if(err){
            throw err;
        }
        res.json(dresses);
    });
});
John
  • 57
  • 7

1 Answers1

0

Give a try to my code

module.exports.getDresses = async function(limit = 100){
    return await Dress.find(callback).limit(limit).exec();
};

Dress = require('./models/dress');

app.get('/api/dresses', function(req, res){
    let dresses = Dress.getDresses();
    res.json(dresses)
});
  • Thanks. Again, returning an empty array. Maybe this is a problem with my database? But I have checked - it is not empty.. – John Jan 30 '19 at 13:08
  • Yes, this might be a problem of my database, because with all this code it is showing me an empty array. – John Jan 30 '19 at 15:03