Reputation:
I have a query which returns an array of result set in node controller. Now I want to loop through the result and I need to call a different function for each result set. How can I achieve this in Node.
I am a new bee in node and couldn't find the solution..
Below is my Router file:
/*jslint node:true*/
var express = require('express');
var session = require('express-session');
var router = express.Router();
var bodyParser = require('body-parser');
const mgrDataCtrl = require('../controllers/mgrDataCtrl');
router.use(session());
// Use body parser to parse JSON body
router.use(bodyParser.json());
// Http Method: GET
router.route('/mgrData/').get(mgrDataCtrl.getMgrData);
module.exports = router;
Below is my Controller:
/*jslint node:true*/
var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');
const database = require('../services/database.js');
// Import events module
var events = require('events');
// Use body parser to parse JSON body
router.use(bodyParser.json());
//Function to get Manager data from youCoach DB
async function getMgrData(req,res,next){
try{
/*Get total items*/
const presult = await database.simpleExecute(`SELECT a.username, a.id, a.email, a.dept_id, b.deptno, b.dept_name from emp a LEFT JOIN dept b ON a.dept_id = b.id`);
const prows = presult.rows;
console.log("Rows ="+prows);
res.json(prows);
**// Here I want to write a for loop for above result and call a different function for each row.**
} catch (err) {
console.log("Error ===="+err);
next(err);
}
};
module.exports.getMgrData = getMgrData;
Thanks in Advance..
Upvotes: 0
Views: 1660
Reputation: 1395
You can use the async js for looping and manage the execution of code. for data result loop you can use async.eachOf
from async.js very easy to implement. and inside a loop, you can write your business logic base on your result.
Here is document link: https://caolan.github.io/async/docs.html#eachOf
Check below code implement of async.eachOf
Hope this will help you.
/*jslint node:true*/
var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');
const database = require('../services/database.js');
// Import events module
var events = require('events');
var async = require("async");
// Use body parser to parse JSON body
router.use(bodyParser.json());
//Function to get Manager data from youCoach DB
async function getMgrData(req,res,next){
try{
/*Get total items*/
const presult = await database.simpleExecute(`SELECT a.username, a.id, a.email, a.dept_id, b.deptno, b.dept_name from emp a LEFT JOIN dept b ON a.dept_id = b.id`);
const prows = presult.rows;
let finalResult = [];
async.eachOf(prows, function(prow, key, eachCallBack) {
finalResult[key] = // output;
eachCallBack();
}, function(err, result){
if(err) {
console.log("Error ===="+err);
next(err);
} else {
res.json(finalResult);
}
})
} catch (err) {
console.log("Error ===="+err);
next(err);
}
};
module.exports.getMgrData = getMgrData;
Upvotes: 1