title: nodejs - cannot search globally cant set headers
body: <p>I try to implement a global search to all the schemas in database, to found doctors or hospitals.</p>
  
  <pre><code>const express = require("express");
  const app = express();
  const Hospital = require("../models/hospital");
  const Medico = require("../models/medico");
  
    app.get("/busqueda/todo/:busqueda", async (req, res, next) =&gt; {
    var busqueda = req.params.busqueda;
    var rex = new RegExp(busqueda, "i");
  
    try {
      let buscaHosp = await Hospital.find({ nombre: rex }, (err, hospitales) =&gt; {
        return res.status(200).json({
          ok: true,
          hospitales: hospitales
        });
      });
  
      if (!buscaHosp) {
        res.json({
          ok: false,
          message: "No hospital."
        });
        return;
      } else {
        let buscarMed = await Medico.find({ nombre: rex }, (err, medicos) =&gt; {
          return res.status(200).json({
            ok: true,
            medicos: medicos
          });
        });
      }
    } catch (error) {
      next(error);
    }
  });
  </code></pre>
  
  <p>busqueda.js</p>
  
  <p>it throw this error:</p>
  
  <blockquote>
    <p>Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
        at ServerResponse.setHeader (_http_outgoing.js:469:11)
        at ServerResponse.header (C:\Users\Leonardo Barahona\Desktop\angular-adv\restserver-adminpro\node_modules\express\lib\response.js:767:10)
        at ServerResponse.send (C:\Users\Leonardo Barahona\Desktop\angular-adv\restserver-adminpro\node_modules\express\lib\response.js:170:12)
        at ServerResponse.json (C:\Users\Leonardo Barahona\Desktop\angular-adv\restserver-adminpro\node_modules\express\lib\response.js:267:15)
        at Medico.find (C:\Users\Leonardo Barahona\Desktop\angular-adv\restserver-adminpro\server\routes\busqueda.js:38:32)
        at C:\Users\Leonardo Barahona\Desktop\angular-adv\restserver-adminpro\node_modules\mongoose\lib\model.js:4530:16
        at process.nextTick (C:\Users\Leonardo Barahona\Desktop\angular-adv\restserver-adminpro\node_modules\mongoose\lib\helpers\query\completeMany.js:35:39)
        at process._tickCallback (internal/process/next_tick.js:61:11)
    Emitted 'error' event at:
        at C:\Users\Leonardo Barahona\Desktop\angular-adv\restserver-adminpro\node_modules\mongoose\lib\model.js:4532:13
        at process.nextTick (C:\Users\Leonardo Barahona\Desktop\angular-adv\restserver-adminpro\node_modules\mongoose\lib\helpers\query\completeMany.js:35:39)
        at process._tickCallback (internal/process/next_tick.js:61:11)</p>
  </blockquote>