V
V
Vaalel2016-10-31 15:50:44
MongoDB
Vaalel, 2016-10-31 15:50:44

Empty array when outputting data from MongoDB, Node.js?

When requesting localhost:3000/api/books , I get an empty array ("[]"). Data Model:
var bookSchema = mongoose.Schema({
title:{
type: String,
required: true
},
genre:{
type: String,
required: true
},
description:{
type: String
},
author:{
type: String,
required: true
},
publisher:{
type: String
},
pages:{
type: String
},
image_url:{
type: String
},
buy_url:{
type: String
},
create_date:{
type: Date,
default: Date. now
});
var Book = module.exports = mongoose.model('Book', bookSchema);
module.exports.getBooks = function(callback, limit){
Book.find(callback).limit(limit);
}
App.js:
Book = require('./models/book');
app.get('/api/books', function(req, res){
Book.getBooks(function(err, books){
if(err){
throw err;
}
res.json(books);
});
})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Yarkov, 2016-10-31
@Valel

Are there records in the database? Try like this:

module.exports.getBooks = function(callback, limit){
    var limit = limit || 10;
    Book.find({}, callback).limit(limit);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question