JavaScript and linked field.

73 views
Skip to first unread message

earthw...@gmail.com

unread,
Jan 23, 2018, 2:47:12 PM1/23/18
to mementodatabase
Hello all! I am a bar manager and am finding tons of use for mementodb. I am not experienced with JavaScript whatsoever but at this it seems simpler to use/read to accomplish what I'm trying to do than working with the calculation field.

So, I have a switch function which should calculate drink dilution based on whether it's shaken or stirred. However, regardless of what the prep method is set to, it outputs the default 0. Any ideas?

var initial_abv = field("Initial ABV")/100;
var prep_method = field("Prep Method");

switch (prep_method) {

case 'Shake':
case 'Dry Shake, Shake':
-1.567 * Math.pow(initial_abv, 2) + 1.742 * initial_abv + 0.203;
break;
case 'Stir':
-1.21 * Math.pow(initial_abv, 2) + 1.246 * initial_abv + 0.145;
break;
default: 0;
}

Bill Crews

unread,
Jan 23, 2018, 3:09:11 PM1/23/18
to earthw...@gmail.com, mementodatabase
Well, you're syntax looks right, so I can only surmise that the field contains some value other than what you think it does and does not match any of your switch values. Did you check? An easy way to check is with message()...

message("Field value is " + yourVariable);

earthw...@gmail.com

unread,
Jan 23, 2018, 3:13:01 PM1/23/18
to mementodatabase
So, I made some sort of progress, but I'm now wondering about how it's possible to get all of the available entries in the linked field array. So, I added the array info at the beginning of the switch statement, but I can only call on an exact number of entries in the array. I need it to see all the available entries that may or may not exist. Hopefully that makes sense.

var initial_abv = field("Initial ABV")/100;
var prep_method = field("Prep Method");

switch (prep_method[0].field("Name")) {

earthw...@gmail.com

unread,
Jan 23, 2018, 4:17:05 PM1/23/18
to mementodatabase
Thanks for your quick reply. I made a second post. I can get the function to work if I select the indexes in the array specifically and add the "Name" field of the linked entry. So, that leads me to believe that everything is working. My problem now is the number of indexes in the array changes so I have to find a way of getting all the available indexes in the array. Any ideas?

Bill Crews

unread,
Jan 23, 2018, 5:23:00 PM1/23/18
to earthw...@gmail.com, mementodatabase
If you're using a one-to-many link, the index/subscript will always be just 0. Only if it is a many-to-many link might the user add more links, in which case you can access then using 0..n-1.

Does that help?

earthw...@gmail.com

unread,
Jan 23, 2018, 6:56:44 PM1/23/18
to mementodatabase
It is a many to many link. That didn't seem to work for me as is. Maybe I'm misunderstanding. This is what I have now.

var initial_abv = field("Initial ABV")/100;

var prep_method = field("Prep Method")[0..n-1].field("Name");

Bill Crews

unread,
Jan 24, 2018, 11:17:03 AM1/24/18
to earthw...@gmail.com, mementodatabase
To run over all links in a many-to-many link to entry field, use the following code...

var initial_abv = field("Initial ABV")/100;
var links = field("Prep Method");
var total = 0;
for (var lnk = 0; lnk < links.length; lnk++) {
   var prep_method = links[lnk].field("Name");
   switch (prep_method) {
   case 'Shake':
   case 'Dry Shake, Shake':
      total += -1.567 * Math.pow(initial_abv, 2) + 1.742 * initial_abv + 0.203;
      break;
   case 'Stir':
      total += -1.21 * Math.pow(initial_abv, 2) + 1.246 * initial_abv + 0.145;
      break;
   default: 0;
   }
total;

I'm assuming that, if there are multiple links, this field should contain the sum of the values from the linked entries.

earthw...@gmail.com

unread,
Jan 24, 2018, 5:51:31 PM1/24/18
to mementodatabase
Bill! You are the best. That works perfectly. Thank you so much!
Reply all
Reply to author
Forward
0 new messages