multiple query in nodejs

0

I want to show a post and the respective comments for this I have 2 tables in mysq l a " post " and the other " comments "

Use mcv model view controller with nodejs and express .

model.js
getOne(id, cb) {
conn.query('SELECT * FROM post WHERE id = ?', id, cb);
}

controller.js 
getOne(req, res, next) {
    let id = req.params.id;
    console.log(id);

    bm.getOne(id, (err, data) => {
        if(!err) {
            res.render('detalles', {
                c: data
            });
        }
    });
}

router.js
router.get('/detalles/:id',bc.getOne);
    
asked by nahuel 13.02.2018 в 20:57
source

1 answer

0

Your question is not very clear, but I think what you need could be done using the async module and using the async.waterfall function to perform several cascade querys and it would be useful if the secondary query depends on the value of the first query.

It would be something like this:

console.log('Program Start');
var async = require('async');
async.waterfall([
  function (callback) {
    console.log('First Step --> ');
    callback(null, '1', '2');
  },
  function (arg1, arg2, callback) {
    console.log('Second Step --> ' + arg1 + ' ' + arg2);
    callback(null, '3');
  },
  function (arg1, callback) {
    console.log('Third Step --> ' + arg1);
    callback(null, 'final result');
  }
  ], function (err, result) {
   console.log('Main Callback --> ' + result);
});

       console.log('Program End');

Greetings

    
answered by 01.04.2018 в 07:08