last day (15 days later) » 

01:58
0
Q: const scoping issue within a switch statement

Brandon McAleesI have the following code in my reducer. In both cases, findFile() returns a result, however, only in the first occurrence (setImageRotation) will "origFile" get set. Changing the name of the second occurrence of "origFile" will allow it to get set. I'm wondering why this is case, as const has a ...

Is that the full code for the cases? origFile is not visible outside of their respective blocks ({...}), so this code is useless.
What are you trying to accomplish that can't be done by declaring the variable outside of the switch?
Please show the complete code, including the switch statement and the place where you intend to use the origFile variable.
I've updated the code, I left it out originally for clarity sake. I'm fairly sure it's irrelevant but I guess we'll see.
What do you mean exactly with "Changing the name of the second occurrence of "origFile" will allow it to get set."?
01:58
origFile is not origfile ...
@AshBelmokadem There are two occurrences of "origFile" - one within setImageRotation and one within setImageRegionOfInterest. The one within setImageRotation will get set fine, the second occurrence will not unless I change "origFile" to something else (ex. "firstFile")
@JonasW. sorry, messed that up in my edit. They're actually the same in my code.
Where are you running this code? Node.js? Browser? Are you transpiling this code? And what version, what browser, what transpiler?
Browser - Chrome v66.0.3359.139
Is this code transpiled? babel? If so, do you have the transpiled code?
You are wondering why two different code paths produce different results? Look at what's different between the two function calls. Have you verified what the value of action is in both cases?
01:58
I really doubt that your analysis is correct, this simple snippet works fine in firefox and chrome. Also note that would be an error if you would create the same const twice or reassign a const
Works fine for me as well, good luck!
@AshBelmokadem added transpiled code, I'm guessing this is where my issue is.
I believe @FelixKling had the correct idea that your action.type might simply not be what you expect it to be. Did you debug? Did you add log statements to see if something comes up? Do you have a simple test to test the handler (the functionality looks basic enough)
I've debugged up to the return value of findFile(). In both cases it returns an object. Only in the first case does it set the const.
"In both cases it returns an object. Only in the first case does it set the const." That's not possible. An assignment doesn't "fail" unless you are trying to assign to an existing const or a non-existing variable. How do you verify that the value is not set? I don't think we can help you much if you cannot produce a Minimal, Complete, and Verifiable example.
But, how come the original code contains origFile but the transpiled code has origfile (lower case f)?
01:58
I suspect the problem is that you're debugging the transpiled code. Notice that the transpiler gives a different name to the variable in the second case.
@FelixKling I suspect that when the transpiler notices variable name conflicts, it uses a canonical naming style for all the replacements, which includes lowercasing it. It could just as easily call them _var1, _var2, etc.
@Barmar it's quite hard to draw a conclusion without a Minimal, Complete, and Verifiable example, so let's close this question till there is one...
@FelixKling I changed the capitalization between the original post and when I generated the transpiled code lol
"I changed the capitalization between the original post and when I generated the transpiled code" I hope you understand that any inconsistency is confusing (and possibly wastes our time investigating) because we cannot know whether it's relevant to the problem or not.
I apologize, forgot to change that back. I've updated the code in the question.
@BrandonMcAlees that doesn't bring us anything, give us a working example that we may test the code you are writing so that what you are saying is also proven by the code you provided, as it stands, your code should work, and the error is elsewhere, and it's up to you to provide the requested example code so we do not have to guess to answer your question.

last day (15 days later) »