Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

189
Views
Nodo JS + MongoDB - ¿Cómo devolver datos del modelo a una ruta?

Estoy comenzando con NodeJS + MongoDB y tengo una pregunta hace un par de días que no pude encontrar la manera de resolverla.

Mi problema es:

Tengo un archivo de ruta que hace una llamada a un modelo, como en el siguiente ejemplo:

Archivo de rutas:

 'use strict'; module.exports = function(app) { app.get('/api/hotels/', function(req, res) { let hotels = new app.model.Hotels(); hotels.pageList(req.params.page, req.params.qtd, (err, docs) => { res.status(404).json({ msg: 'implementation not found' }) }) }) }

Archivo de modelos:

 'use strict'; function Hotels() { this._hotels = process.db.get().collection('hotels') this._ObjectID = process.db.ObjectID() } Hotels.prototype.pageList = function(page, qtd, cb) { //list all hotels this._hotels.find(function(err, hotels) { if (err) { return err; } else { return hotels; } }) cb() } module.exports = function() { return Hotels }

El problema es que no estoy seguro de cómo puedo devolver el resultado del modelo a la ruta y mostrar el objeto JSON en el navegador del usuario.

¿Alguien puede ayudarme?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Pruébalo así:

 Hotels.prototype.pageList = function(page, qtd, cb) { //list all hotels this._hotels.find(function(err, hotels) { cb(err, hotels); }); }

Luego, en tu ruta puedes hacer esto:

 app.get('/api/hotels/', function(req, res) { let hotels = new app.model.Hotels(); hotels.pageList(req.params.page, req.params.qtd, (err, hotels) => { if(err) res.status(404).json({ msg: 'implementation not found' }); res.json(hotels); }) }) }
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!