danielo515 / obsidian-modal-form

Define forms for filling data that you will be able to open from anywhere you can run JS
https://danielorodriguez.com/obsidian-modal-form/
MIT License
203 stars 17 forks source link

[BUG] result.get is not a function #162

Closed the1gofer closed 9 months ago

the1gofer commented 9 months ago

Templater throws the following error:

plugin:templater-obsidian:61 Templater Error: Template parsing error, aborting. 
 result.get is not a function

To Reproduce

const modalForm = app.plugins.plugins.modalforms.api;
const result = await modalForm.openForm("new-case");
let leadDefendant = result.get("defendants");
{
  "title": "New Case",
  "name": "new-case",
  "customClassname": "case",
  "fields": [
    {
      "name": "defendants",
      "label": "Defendants",
      "description": "",
      "isRequired": true,
      "input": {
        "type": "multiselect",
        "folder": "TCPA/1 - Cases/52 - Entities of Interest",
        "query": "",
        "multi_select_options": [],
        "source": "notes"
      }
    },
    {
      "name": "attorney",
      "label": "Attorney",
      "description": "Attorney",
      "isRequired": false,
      "input": {
        "type": "multiselect",
        "source": "notes",
        "folder": "TCPA/1 - Cases/52 - Entities of Interest",
        "query": "",
        "multi_select_options": []
      }
    },
    {
      "name": "related parties",
      "label": "Related Parties",
      "description": "Related Parties",
      "isRequired": false,
      "input": {
        "type": "multiselect",
        "source": "notes",
        "folder": "TCPA/1 - Cases/52 - Entities of Interest",
        "query": "",
        "multi_select_options": []
      }
    }
  ],
  "version": "1"
}

Expected behavior assign value of defendants to variable

Additional context It might be something I'm doing, but I don't see it.

danielo515 commented 9 months ago

What version of modal forms are you using? the get method was introduced in 1.31 I tested your form and code and it works fine

the1gofer commented 9 months ago

1.29

the1gofer commented 9 months ago

Ok, works after updating.

the1gofer commented 9 months ago

for multi select, are their any methods to get just the text, or individual results? for example result.get("defendants")[0] or result.get("defendants").text. something like that.

danielo515 commented 9 months ago

The get method takes an optional mapping function you can use to modify the values. So, if you have your values "nested" in an object, you can do something like this.

Imagine this is just a single value but want the text property:

result.get('something', (thing) => thing.text)

If you have several objects and want to join them

result.get('something', (thing) => thing.join(', ')

If you want to join and transform them at the same time, you can do this:

result.get('somethings', (things) => thing.map((i) => `[[${i}]]`).join(', '))
the1gofer commented 8 months ago

so

let caseName = result.get('case-link') works, and gives a result like '["something"]'

however let caseName = result.get('case-link', (thing) => thing.text) gives a result of "undefined"

the1gofer commented 8 months ago

ok so it looks like the problem is .text

so let caseName = result.get('case-link', (thing) => thing)

has the desired result

danielo515 commented 8 months ago

That is correct. The mapping/transform function is optional,in case you don't provide it what you get back is just the value. On top of that, the mapping function is only called if the value exists, and it will get the plain value, nothing wrapping it.

I'm working in a more advanced and convenient API ,you can take a look at the documentation in the PR if you have to see a preview of how it will work: #178

danielo515 commented 8 months ago

I see how my first comment could lead to confusion. There is no situation right now where you will get an object with a text value, so my first example was a bit confusing. For completeness , you can also call it with just the key name you want

result.get('something')

Or a more logical transformation could be

result.get('something', (it) => it.toUpperCase())