# Nodejs - forEach(function) - newbie question

**URL:** <https://forum.kirupa.com/t/nodejs-foreach-function-newbie-question/635131>\
**Category:** Uncategorized\
**Created:** [August 17, 2016, 11:50pm UTC](https://forum.kirupa.com/t/nodejs-foreach-function-newbie-question/635131 "2016-08-17T23:50:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wise\_Bulls](https://avatars.discourse-cdn.com/v4/letter/w/4bbf92/32.png) [@Wise\_Bulls](https://forum.kirupa.com/u/Wise_Bulls)\
**Post date:** [August 17, 2016, 11:50pm UTC](https://forum.kirupa.com/t/nodejs-foreach-function-newbie-question/635131/1 "2016-08-17T23:50:59Z")

</div>

Hi,

I have a mysql database table called MASTER which have around 10 columns - with the field ‘TS’ being a primary key. I want to write a function wherein three fields - TS, MWP & LTP - from every row are retrieved from the table and stored as a variable “tmpAnalysis” - based on which some rules are run.

The below code is generating only the first row from the MASTER table - I want it to run for each row. What should I be doing?

Thanks

* * *

```
funcE = function (MasterGet) {
	var countCheckRule = 0;
	var countCheckedRule = 0;
	var tmpAnalysis = {};
	rules.rule12(tmpAnalysis, function (_tmpAnalysis) {
		tmpAnalysis = _tmpAnalysis;
	});
	news.analysis = tmpAnalysis;
	connection.query('SELECT * FROM MASTER', function selectCb(error, results, fields) {
		if (error) {
			console.log('402-GetData Error: ' + error.message);
			return;
		}
		if (results.length > 0) {
			results.forEach(function (value) {
				var firstResult = results[0];
				console.log('403-funcE from firstresult TS: ' + firstResult['TS'] + 'MWP: ' + firstResult['MWP'] + 'LTP: ' + firstResult['LTP']);
				news.Master = firstResult;
				io.emit('news', news);
				var TS = firstResult['TS'];
				var MWP = firstResult['MWP'];
				var LTP = firstResult['LTP'];
				rules.checRules(firstResult, myhttp, function (rules_res) {
					console.log('404-Rules check triggered');
					firstResult.rules = rules_res;
				})
			});
		}
	});
	countCheckRule++;
	console.log('405-funcE ends - funcA called');
	funcA();
};

```

* * *

---

<div class="post-metadata">

**Author:** ![kadaj](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kadaj/32/9061_2.png) [@kadaj](https://forum.kirupa.com/u/kadaj)\
**Post date:** [October 12, 2016, 6:49pm UTC](https://forum.kirupa.com/t/nodejs-foreach-function-newbie-question/635131/2 "2016-10-12T18:49:13Z")

</div>

You should be using the variable `value` instead of using `results[0]` in the `forEach` function.
