Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

226
Vistas
I have request data from 2 database. How can I merge code to one function?

I have request data from 2 database. But I need to merge 2 request in one function and send data to result.html

function showResult(req, res){
        var n = req.query.query
        mysql_conn.query('SELECT query_text FROM catalogsearch_query WHERE query_text LIKE "%' + n + '%" ORDER BY popularity DESC LIMIT 0 , 10', function(error, rows) {
        res.render('result.html',{result:n , related: rows.map(row => row.query_text)})
    })
}

Need to merge with this code

mysql_crawl.query('SELECT prod_name, full_price, discount_price, quantity, prod_link, images, prod_desc, status FROM `catalogsearch_fulltext` WHERE MATCH(data_index) AGAINST("cream") LIMIT 0 , 10', function(error, product_data){
res.render('result.html',{product_data: product_data})

How can I merge code together in one function?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Because it is asynchronous, we have to wait for till we have both responses to execute the render. (If the mysql module supports promises, you can do it another way.).

var n = req.query.query;
mysql_conn.query('SELECT query_text FROM catalogsearch_query WHERE query_text LIKE "%' + n + '%" ORDER BY popularity DESC LIMIT 0 , 10', function (error, rows) {

    // at this point, only the first query has been executed

    mysql_crawl.query('SELECT prod_name, full_price, discount_price, quantity, prod_link, images, prod_desc, status FROM `catalogsearch_fulltext` WHERE MATCH(data_index) AGAINST("cream") LIMIT 0 , 10', function(error, product_data) {

        // at this point, both queries should have been executed

        res.render('result.html', {
           result: n, 
           related: rows.map(row => row.query_text),
           product_data: product_data
        })
    });
});

I never used this module, but be careful of SQL injection.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda