Node JS scope issue

I've attached a code file since I'm not able to get past the "i" issues
node-scope-issue.txt
kevbob650Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
You're not returning anything form your function.  Try defining the 'result' variables before the function.
kevbob650Author Commented:
I'm just trying to console.log.  When I console.log from within the curl.request I'm good, other than that I get undefined.  Excuse my comment marks, var resultArray = new Array() is set both in and out of the function.
Dave BaldwinFixer of ProblemsCommented:
Using 'var' both times clears it the second time at least.  'var' is to define a 'new' variable... or redefine an old one.  And the 'console' statement inside the 'curl.request()' is using the variable defined Inside that function which I think is why you are not seeing it the second time.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

kevbob650Author Commented:
Sorry my code file was not clear. I'm showing all of the combinations I've tried.  When I use the global setting, I did not use the local one and vice-versa.  I only declared it once and use it accordingly, that's not the issue.
RobOwner (Aidellio)Commented:
curl.request(options, callback);

Node.js is asynchronous so your array is not going to be defined as the curl hasn't finished yet so put it in the callback.
You're not actually inside the curl request, but inside the callback function.  The way you have it is correct.
kevbob650Author Commented:
Thanks for the async idea.  But is it not already in a callback??  Maybe I'm misunderstanding the callback!
curl.request(options, function (err, teams) {.     isn't the 'function' a callback?
RobOwner (Aidellio)Commented:
yes that "function" is run when the curl request has completed what it needs to do... Was that not your issue?
kevbob650Author Commented:
Yes, sorry for my async dumminess...  So the curls request grabs some data from a perl file and I wrap it up into an array. I can see a problem with a 'return' statement, but I would think I could console.log it??   So how would I do a callback that works?
RobOwner (Aidellio)Commented:
no stupid questions here so please don't apologise!

you can and have console.log'd it:

curl.request(options, function (err, teams) {
		var teamObject = JSON.parse(teams);
		var resultArray = new Array();     // local doesn't work either
		var resultObject = {};
		for(var i = 0; i < ticketArray.length; i++) {
			var t = teamObject[0][ticketArray[i]];
			for (var j in t) {
			  resultObject[ticketArray[i]] = {'Name': t[j].teamName, 'ID' : t[j].ID };
			  resultArray.push(resultObject);
			}
		}
======>		console.log(resultArray);   <===========// able to console here inside the curl.request[/b]
	});

Open in new window


I wonder if it makes more sense like this, as this is EXACTLY the same as above:

// perform your curl request.  myCallback is called when the request is complete.
curl.request(options, myCallback);

// here is your callback below.
function myCallback(err, teams) {
                var teamObject = JSON.parse(teams);
		var resultArray = new Array();     // local doesn't work either
		var resultObject = {};
		for(var i = 0; i < ticketArray.length; i++) {
			var t = teamObject[0][ticketArray[i]];
			for (var j in t) {
			  resultObject[ticketArray[i]] = {'Name': t[j].teamName, 'ID' : t[j].ID };
			  resultArray.push(resultObject);
			}
		}
		console.log(resultArray);
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kevbob650Author Commented:
Ah, I see it now. Your last entry makes sense. Thanks so much for your patience!  Good work!
RobOwner (Aidellio)Commented:
Good stuff!  Yeah it took me a while to get my head around how Node operates with all the async stuff.  It's a different way of thinking versus your typical PHP server side script.
kevbob650Author Commented:
So true.  Async doesn't always sync with my out of sync brain. Thanks again.
RobOwner (Aidellio)Commented:
No problem. Thanks for the points
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.