Variable not being set correctly

HI All,

I am trying to set up a data entry screen that accepts various fields and creates a tiddler with a title created out of some of the supplied field values and a set of tags, also from various field values in addition to [[All Items]] and [[Needs History]] (see screen shot below)

All of this seems to work OK except for one value, the name of the state in which the item is housed.

A cut down test wiki can be accessed at http://cultconv.neocities.org/test.html

The problem occurs in the $:/TLS/Actions/Add New Item tiddler, whose code is

Add the information for the new item below and press the //Add this item// button

The asset number for this new object will be {{{ [{$:/TLS/incremental}] }}}

State Code: <$select tiddler='$:/TLS/selectedstate'  field='state-code'>
{{$:/TLS/state_for_select}}
</$select>
OS for overseas, PRI for private

<$reveal type="match" stateTitle="$:/TLS/selectedstate" stateField="state-code" text="-">
Error - the selected state code can not be '-'
</$reveal>

<$reveal type="nomatch" stateTitle="$:/TLS/selectedstate" stateField="state-code" text="-">

City/Town: <$edit-text tiddler="$:/TLS/city" tag="input"/>

Location: <$edit-text tiddler="$:/TLS/location" tag="input"/>

Type:  <$edit-text tiddler="$:/TLS/type" tag="input" /> If unknown, enter 'type unknown'


-----

<$button>

<$let 
selectedstatecode={{$:/TLS/selectedstate!!state-code}} 
selectedstatename={{{ [<selectedstatecode>match[NT]then[Northern Territory]] }}}
selectedstatename={{{ [<selectedstatecode>match[WA]then[Western Australia]] }}}
selectedstatename={{{ [<selectedstatecode>match[SA]then[South Australia]] }}}
selectedstatename={{{ [<selectedstatecode>match[VIC]then[Victoria]] }}}
selectedstatename={{{ [<selectedstatecode>match[NSW]then[New South Wales]] }}}
selectedstatename={{{ [<selectedstatecode>match[QLD]then[Queensland]] }}}
selectedstatename={{{ [<selectedstatecode}>]match[TAS]then[Tasmania]] }}}
selectedstatename={{{ [<selectedstatecode>match[ACT]then[Australian Capital Territory]] }}}
selectedstatename={{{ [<selectedstatecode>match[PRI]then[Private]] }}}
selectedstatename={{{ [<selectedstatecode>match[OS]then[Overseas]] }}}

  locationTag={{$:/TLS/location}}
  typeTag={{$:/TLS/type}}
  cityTag={{$:/TLS/city}}
>

     <$action-createtiddler 
          $basetitle={{{ [{$:/TLS/incremental}addsuffix[:]addsuffix{<selectedstatename>}addsuffix[ - ]addsuffix{$:/TLS/type}addprefix{$:/TLS/selectedstate!!state-code}] }}}
          tags={{{ [[All Items]] [[Needs History]] [<cityTag>] [<selectedstatename >] [<locationTag>] [<typeTag>] +[format:titlelist[]join[ ]] }}}
          CityTown={{$:/TLS/city}}
          Location={{$:/TLS/location}}
           State=<<selectedstatename>>
          GunType={{$:/TLS/type}}
           $template="$:/TLS/CreateTemplateItems"
     />

<$action-navigate $to={{{ [{$:/TLS/incremental}addsuffix[:]addsuffix{$:/TLS/selectedstate!!state-name}addsuffix[ - ]addsuffix{$:/TLS/type}addprefix{$:/TLS/selectedstate!!state-code}] }}} />

<$action-setfield $tiddler="$:/TLS/dateofmanufacture" text=""/>
<$action-setfield $tiddler="$:/TLS/city" text=""/>
<$action-setfield $tiddler="$:/TLS/location" text=""/>
<$action-setfield $tiddler="$:/TLS/manufacturer" text=""/>
<$action-setfield $tiddler="$:/TLS/serialnumber" text=""/>
<$action-setfield $tiddler="$:/TLS/weightofprojectile" text=""/>
<$action-setfield $tiddler="$:/TLS/range" text=""/>
<$action-setfield $tiddler="$:/TLS/caption" text=""/>
<$action-setfield $tiddler="$:/TLS/imageURL" text=""/>
<$action-setfield $tiddler="$:/TLS/coordinates" text=""/>
<$action-setfield $tiddler="$:/TLS/calibre" text=""/>
<$action-setfield $tiddler="$:/TLS/type" text=""/>
<$action-setfield $tiddler="$:/TLS/selectedstate" $field="state-code" $value=""/>
<$action-setfield $tiddler="$:/TLS/selectedstate" $field="state-name" $value=""/>


<$action-setfield $tiddler="$:/TLS/incremental" text={{{ [{$:/TLS/incremental}add[1]] }}}/>

</$let>

Add new Item
</$button>
</$reveal>

The area I think the problem is in is the $let statement

<$let 
selectedstatecode={{$:/TLS/selectedstate!!state-code}} 
selectedstatename={{{ [<selectedstatecode>match[NT]then[Northern Territory]] }}}
selectedstatename={{{ [<selectedstatecode>match[WA]then[Western Australia]] }}}
selectedstatename={{{ [<selectedstatecode>match[SA]then[South Australia]] }}}
selectedstatename={{{ [<selectedstatecode>match[VIC]then[Victoria]] }}}
selectedstatename={{{ [<selectedstatecode>match[NSW]then[New South Wales]] }}}
selectedstatename={{{ [<selectedstatecode>match[QLD]then[Queensland]] }}}
selectedstatename={{{ [<selectedstatecode>]match[TAS]then[Tasmania]] }}}
selectedstatename={{{ [<selectedstatecode>match[ACT]then[Australian Capital Territory]] }}}
selectedstatename={{{ [<selectedstatecode>match[PRI]then[Private]] }}}
selectedstatename={{{ [<selectedstatecode>match[OS]then[Overseas]] }}}

  locationTag={{$:/TLS/location}}
  typeTag={{$:/TLS/type}}
  cityTag={{$:/TLS/city}}
>

It would appear that the variable, selectedstatename, is not being set by any of these assignment statements.

@CasperBooWisdom suggested the form of the statements to set selectedstatename as this is a simpler form of the table lookup I have been trying, which also did not work.

As you can see from the attached, if I enter statecode, town,location and type, a tiddler with the title as shown is created which misses the tag, South Australia and also has an empty state field. The state name is also missing in the title.

Interestingly, the other tags and their variables are correctly created.

The only difference that I can see is that all except for selectedstatename are provided through the entry screen whilst the selectedstatename is assigned according to the value of the statecode in the entry screen.

I have tried replacing the assigment lines to the form, replacing selectedstatecode by $:/TLS/selectedstate!!state-code

selectedstatename={{{ [{$:/TLS/selectedstate!!state-code}match[NT]then[Northern Territory]] }}}
This also does not work.

@pmario suggested moving the button action code into a procedure which I tried but it also did not work.

Loging variables indicates that selectedstatename is never updated.

I am hoping someone can tell me what I am doing wrong.

bobj

Ahh, when I was aswering your previous question associated with this, I did not realize you were going to be setting the variable multiple times. The code you have does not work like a switch/case statement in other programming languages, but rather, what you are doing is basically setting the varible to nothing, unless the selectedcode is OS, since that is the last choice you have, so that is the only time it would match without being overwritten by an empty filter reponse. Each iteration of this is being run through, so everytime it does not match, the variable is set to empty, or no result, so if selectedstatecode was “ACT” for instance, it would then try to check if it matchs PRI, right below it, which would once again set the variable to empty, blank, because there is no “truthy” result.

You should create a tiddler of type “application/json” with the following:

{
	"WA":"Western Australia",
	"SA":"South Australia",
	"VIC":"Victoria",
	"NSW":"New South Wales",
	"QLD":"Queensland",
	"TAS":"Tasmania",
	"ACT":"Australian Capital Territory",
	"PRI":"Private",
	"OS":"Overseas"
}

Lets say it is called $:/TLS/StateData

Then simply do this in your $:/TLS/Actions/Add New Item tiddler:

<$let 
    selectedstatecode={{$:/TLS/selectedstate!!state-code}} 
    selectedstatename={{{ [{$:/TLS/StateData}jsonget<selectedstatecode>] }}}

    locationTag={{$:/TLS/location}}
    typeTag={{$:/TLS/type}}
    cityTag={{$:/TLS/city}}
>

Yes, the problem is in the $let statement. Each of the selectedstatename={{{ [...] }}} says, in effect: "if the selectedstatecode has a value of ‘somecode’, then set the selectedstatename to ‘somename’ OTHERWISE, SET THE selectedstatename to BLANK. As a result, as each assignment is done, if the code value doesn’t match, it CLEARS any previously assigned statename. The only assignment that works as intended is the last one… if you select “OS” from the list, then the selectedstatename IS correctly set to “Overseas”.

I see that @CasperBooWisdom has already suggested one possible fix for this problem.

There is also a syntax error in the filter you use for contructing the $basetitle:

  • This: addsuffix{<selectedstatename>} should be: addsuffix<selectedstatename>
    (i.e., no curly brackets)

You can also take advantage of a feature of the $action-createtiddler widget… within the BODY of that widget, you can use createTiddler-title to refer to the title of the tiddler that was just created. Thus, you can write your code like this:

<$action-createtiddler 
   $basetitle={{{ [{$:/TLS/incremental}addsuffix[:]addsuffix<selectedstatename>addsuffix[ - ]addsuffix{$:/TLS/type}addprefix{$:/TLS/selectedstate!!state-code}] }}}
   tags={{{ [[All Items]] [[Needs History]] [<cityTag>] [<selectedstatename >] [<locationTag>] [<typeTag>] +[format:titlelist[]join[ ]] }}}
   CityTown={{$:/TLS/city}}
   Location={{$:/TLS/location}}
   State=<<selectedstatename>>
   GunType={{$:/TLS/type}}
   $template="$:/TLS/CreateTemplateItems">
   <$action-navigate $to=<<createTiddler-title>>/>
</$action-createtiddler>

Note how the last line of $action-createtiddler ends with a > rather than a />. This places the subsequent $action-navigate widget within the body of the enclosing $action-createtiddler widget, where the <<createtiddler-title>> variable is available for use as the $to=... parameter value. Then, following the $action-navigate, the enclosing $action-createtiddler widget is closed by using </$action-createtiddler>

-e

@CasperBooWisdom , I have not heard of application json before. Will follow it up.

Thanks

Bobj

Thanks @EricShulman clear as usual.

Bobj

The “application/json” is what you set the “type” field of the tiddler to. The default type is “text/vnd.tiddlywiki”, but if you set it to “application/json”, you can use that tiddler as a data tiddler, and access the values by calling its index, in your case, the index would be the selectedstatecode and the returned value would be the selectedstatename.

Here is the documentation for accessing JSON data tiddlers:
https://tiddlywiki.com/static/Reading%20data%20from%20JSON%20tiddlers.html

@CasperBooWisdom and @EricShulman

I don’t know how to thank you both enough. Days of frustration have just evaporated.

I wish I had know of application/json tiddlers before. I had come across data tiddlers before but was unaware of how to use them.

They will certainly figure more prominently from now on.

bobj

Just for completeness, JSON tiddlers is one of two types of data tiddlers. The original is dictionary tiddlers, which is (I think) a little bit simpler, and what the core uses for most of it’s lookups.

DataTiddlers: TiddlyWiki v5.3.6 — a non-linear personal web notebook

Agreed. I used to use the standard datatiddler in the beginning, but my nodejs tiddlywikis have become a lot more intricate since then, and I extensively use the Webserver API as well as tm-http-request, pushing, pulling and manipulating tiddlers for use in other external web applications, the JSON data tiddlers are more versatile and easier to manipulate, convert, etc, making it a bit easier for data exchange, so without even thinking, I default to using JSON tiddlers for my data examples.

@Bob_Jansen has a more simple purpose for the data, so yeah, the basic datatiddler is more than sufficient for his use case.

To use a “basic datatiddler”, the text contents of $:/TLS/StateData would be written like this:

WA:Western Australia
SA:South Australia
VIC:Victoria
NSW:New South Wales
QLD:Queensland
TAS:Tasmania
ACT:Australian Capital Territory
PRI:Private
OS:Overseas

Note the much simpler “one entry per line” syntax that avoids the potential for errors due to missing or misplaced curly braces, quotes, and commas. Note also that the tiddler’s type field needs to be set to application/x-tiddler-dictionary instead of application/json. Then, to perform the “lookup”, you would use:

{{{ [[$:/TLS/StateData]getindex<selectedstatecode>] }}}<br>

-e

I feel as though there is a fundamental maintenance issue with this approach: Dealing with state codes and names involves at least three separate parts.

That means there is no single source of truth for State names and codes. That always sets off warning bells for me.

To my mind, a much better approach is to derive all this information from the state tiddler, … which already have it! (In the state-name and state-code fields, or in the title and state-code fields, depending on which version of the state tiddlers are being used.)

In the other thread, I gave my suggestion for a different approach. One of the things it does is to address this problem. It uses the state tiddlers as the source of truth for the list of states and for the mapping of state code to state name. That feels much more maintainable to me.

1 Like

@Scott_Sauyet , your comments are valid. However, the initial data model was an attempt at doing a mapping table. With the use of data tiddlers, this now disappears and so the maintenance issue and code is simplified. However, it must be recognised that the names and number of Australian states are unlikely to be changed and so their maintenance issue almost non existent. I guess there could be additional faux-states, li,e private and overseas, but not in the current data set.

As for the pop up, I guess it would be possible to construct the pop up items from the data tiddler and I will look at that.

Thanks for your thoughtful comments and ideas though, it greatly assisted in Sorting this out.

Bobj

1 Like