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);
    });
});
2
  • 1
    what do you want to send via find ? the callback is not valid finder Commented Jan 29, 2019 at 16:12
  • function(err, dresses) is being sent as a callback. Commented Jan 29, 2019 at 16:15

2 Answers 2

2

example how to use find via mongoose :

// named john and at least 18
MyModel.find({ name: 'john', age: { $gte: 18 }});

// executes immediately, passing results to callback
MyModel.find({ name: 'john', age: { $gte: 18 }}, function (err, docs) {});

// name LIKE john and only selecting the "name" and "friends" fields, executing immediately
MyModel.find({ name: /john/i }, 'name friends', function (err, docs) { })

// passing options
MyModel.find({ name: /john/i }, null, { skip: 10 })

// passing options and executing immediately
MyModel.find({ name: /john/i }, null, { skip: 10 }, function (err, docs) {});

// executing a query explicitly
var query = MyModel.find({ name: /john/i }, null, { skip: 10 })
query.exec(function (err, docs) {});

// using the promise returned from executing a query
var query = MyModel.find({ name: /john/i }, null, { skip: 10 });
var promise = query.exec();
promise.addBack(function (err, docs) {});

taken from link

Sign up to request clarification or add additional context in comments.

1 Comment

I need to get all results in json format, but not the concrete ones
0

try this:

Dresses schema :

const mongoose = require("mongoose");

const Schema = mongoose.Schema;

const Dress= new Schema({
 title:{
    type: String,
    required: true
},
description:{
    type: String,
    required: true
},
price:{
    type: String
},
stock:{
    type: String
},
location:{
    country:{
        type: String
    },
    city:{
        type: String
    },
    street:{
        type: String
    },
    lon:{
        type: String
    },
    lat:{
        type: String
    }
}
});

module.exports = mongoose.model("dress", Dress);

Get dresses from database:

const {Dress} = require('./models/dress');

Dress.find().then(result => {

    console.log(result);

    });

5 Comments

then check your database if you have Data or not!
my database is not empty. but there is an error within this output. so this is not the solution.
please write the dresses from database full code, because this might be the problem.
what is the Error in console please ?
you shud use express to access all functions like find

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.