Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

178
Visualizações
how to get request by paasing an id

I am trying to get an element from the database by paasing the id of that element in the request. I have a controller file where i defined this function

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 })
    }
  }
}

and a DAO file where i defined this function

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
    }
  }
}

I get this error

Something went wrong in getRestaurantByID: TypeError: reports.get is not a function api, TypeError: reports.get is not a function

Any why get is not a fucntion?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

From the documentation of mongodb, there is no method for get in collection. You may use find.

Also, you may just change the reports to static property of the class for better encapsulation.

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda