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

177
Views
cómo obtener una solicitud pasando una identificación

Estoy tratando de obtener un elemento de la base de datos pasando la identificación de ese elemento en la solicitud. Tengo un archivo de controlador donde definí esta función.

 import ReportsDAO from "../dao/reportsDAO.js" export default class ReportsController { static async apiGetReportById(req, res, next) { try { const reportId = req.query.id console.log(reportId) const report = await ReportsDAO.getReportByID(reportId) if (!report) { res.status(404).json({ error: "Not found" }) return } res.json(report) } catch (e) { console.log(`api, ${e}`) res.status(500).json({ error: e }) } } }

y un archivo DAO donde definí esta función

 import mongodb from "mongodb" const ObjectId = mongodb.ObjectID let reports export default class ReportsDAO { // data access objects for other crud methods static async injectDB(conn) { if (reports) { return } try { // will create it auto if dosen't exits reports = await conn.db(process.env.RESTREVIEWS_NS).collection("reports") } catch (e) { console.error(`Unable to establish collection handles in userDAO: ${e}`) } } static async getReportByID(reportId) { try { const getOneSingleReport = await reports.get ({ _id: ObjectId(reportId), }) return getOneSingleReport } catch (e) { console.error(`Something went wrong in getRestaurantByID: ${e}`) throw e } } }

me sale este error

Algo salió mal en getRestaurantByID: TypeError: reports.get no es una función api, TypeError: reports.get no es una función

¿Por qué get no es una función?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

De ladocumentación de mongodb , no hay ningún método para get en la colección. Puede usar find .

Además, puede simplemente cambiar los reports a la propiedad estática de la clase para una mejor encapsulación.

 import mongodb from "mongodb"; const ObjectId = mongodb.ObjectID; export default class ReportsDAO { static reports; // data access objects for other crud methods static async injectDB(conn) { if (this.reports) { return; } try { // will create it auto if dosen't exits this.reports = await conn .db(process.env.RESTREVIEWS_NS) .collection("reports"); } catch (e) { console.error( `Unable to establish collection handles in userDAO: ${e}` ); } } static async getReportByID(reportId) { try { const getOneSingleReport = await this.reports.find({ _id: ObjectId(reportId), }); return getOneSingleReport; } catch (e) { console.error(`Something went wrong in getRestaurantByID: ${e}`); throw e; } } }
about 4 years ago · Juan Pablo Isaza 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!