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);
    });
});
1
  • asynchronous behaviour Commented Jan 30, 2019 at 9:21

1 Answer 1

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)
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Again, returning an empty array. Maybe this is a problem with my database? But I have checked - it is not empty..
Yes, this might be a problem of my database, because with all this code it is showing me an empty array.

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.