I can not display my mongodb data with mongoose in node.js

1

//myapp/routes/index.js
//declaramos las dependencias
var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose'),
	Schema =mongoose.Schema;
var router = express.Router();
mongoose.connect("mongodb://localhost/Libros",function(err){
	if(err){

	}else{
		console.log('conectado a mongodb');
	}
});

var LibrosSchema =new Schema({
	nombre: String,
	anio: Number,
	autor: String,
	pais: String,
	Region:String,
	descripcion:String,
	foto: String
});

var Libross = mongoose.model('Libros',LibrosSchema);
/* GET home page. */
router.get('/', function(req, res, next) {
	Libross.find(function(err, doc){
		console.log(doc);
		console.log("resivido");
		//res.send(Libros);
	});
  //res.render('index', { title: 'Express' });
  res.send('respond with a resource');
});

module.exports = router;

    
asked by Alvaro 31.03.2017 в 17:21
source

1 answer

0

You are misusing the find function. This function works the same as with native MongoDB, that is, accepts as the first parameter an object which is the filtering that MongoDB will use to obtain the documents that match this filter. As a second parameter, it accepts a function in case you want to use callbacks. If a callback is not specified, then find will return a promise.

Callback

Libross.find({}, (err, libros) => {
  res.render('libros', { libros });
});

Promise

Libross.find({}).then(libros => res.render('libros', { libros });

PS: it is written received not resivido .

    
answered by 03.04.2017 в 18:11