I want to set a client scope variable which is a combination of text and the output from a function. No matter how I format the cfset I can't get the result I need.
I am using session management and the users are authenticated against database tables. So I use the GetAuthUser() function to get the user name.
In my application.cfc I want to prefix the username with a pathname string. But I can't seem to get the syntax correct. I always end up with only the string. I start and stop the Coldfusion sever to make sure the client scope is cleared but it makes no difference.
I would prefer to use cfset for this and not cfscript.
This is the function in my application.cfc:
<cffunction name="OnSessionStart" >
<cfset client.customerpath="C:\Dropbox\CustomerArea\" & #GetAuthUser()#>
</cffunction>
A cfdump shows only C:\Dropbox\CustomerArea\
The GetAuthUser() on it's own works fine. I just can't seem to prefix it.
First, verify that your Login is working correctly. I believe that your code above is functioning correctly, but the login is breaking down somewhere, so your
getAuthUser()is blank.After that is verified, your above concatenation should work as expected.
Or, there are a couple of other ways to concatenate a string. Off the top of my head:
Your method:
Direct concatenation: (NOTE: In modern versions of CF, this one should be pretty quick for multiple calls.)
The shorthand of your method:
And the Java StringBuffer:
Each method will have different performance, and you will have to test in your system to see which is most performant.
https://trycf.com/gist/22fc36dcbb8653d0b32ceb22987bf2d5/acf?theme=monokai
NOTE: You might also want to provide a default directory to prevent accidental assignment to a higher directory. (If CF11+, you can use the elvis operator to make this easy. https://trycf.com/gist/0b0d513b3e45af427f4813099e84c9c9/acf11?theme=monokai)
And on a personal opinion note, I don't know why you would prefer
<cfset...>over usingcfscript. This is one of the things thatcfscriptmakes exceptionally easy and clear. Plus changing fromcftagstocfscriptwill open up a lot of benefits to ColdFusion.