| Reference | Downloads | Github

"undefined" in place of string

Various instructions throughout the trial refer to a person’s name, which should be retrieved from the conditions file variable “nm” on each trial

Online it’s showing up as “undefined”, so my ‘begin routine’ code:

pracPretrialText ='The following person is ' + nm + ', she will appear on-screen for a very short period of time, so please pay close attention. Click to continue.'

Shows up as “The following person is undefined, she will…” etc.

The text component has $pracPretrialText in the text field, set every repeat

Originally I had the whole line of code in the text field itself rather than creating the variable in a separate code component. Changing to a code component fixed a similar issue with my images, the path of which is set by concatenating a string much like the above, but this hasn’t fixed the text.

Where is the code setting nm I’m relation to this code setting the text and the text component itself?

The code component is in the same routine as the text component, the variable is created in the “begin routine” tab. Not sure whether the vertical position makes a difference but it’s at the top.

It looks as though the variable is being created at the right time as it has the rest of the string correctly formed, and other parameters from the conditions file are working as expected (e.g. which image to display)

I would expect it to work if the code component is above the text component.

However, another way to do the same thing is to put text.text=… in the code component and just have the text component as blank.

Thanks, I’ll give that a try and see if it helps

It looks like the issue was spaces in the names in the excel sheet, I’ve changed it now so the condition file specifies “name1”, “name2” etc. and the actual string is defined later, e.g:

if name == 'name1':
    nm = 'Firstname Surname'

Do you mean to have name1 in quotes? Is it supposed to be a variable (from Excel) or a literal string?

In Excel it’s just name1 in the cell without quotes. Having the full name in the cell didn’t seem to work with or without quotes

Try using something other than name as your other variable. That might be a protected word. I’d need to see some more Python code to work out how your variables are put together.

Sorry I should have been clearer before: it is now working as expected - using name1, name2 etc. in the conditions file and then using if statements to specify the string to display has fixed the issue

The only difference I can see from the console in-browser is that previously when I ran console.log(nm) it returned the name in quotes: “Firstname Surname”, but now that I have changed it, it returns: Firstname Surname

I’m not familiar enough with how JavaScript handles different datatypes to know what was wrong with the former