FooSoft / anki-connect

Anki plugin to expose a remote API for creating flash cards.
https://foosoft.net/projects/anki-connect/
Other
1.92k stars 218 forks source link

CreateModel gives "Card template ... has a problem.<br>See the preview for more information." #413

Closed skint007 closed 8 months ago

skint007 commented 8 months ago

I have a PowerShell script that reads templates and imports them into Anki. I'm currently having an issue with importing one of them and the error given isn't very descriptive.

I've taken a look at the Card template has a problem page in the Anki docs but all of those have descriptions.

This is the json I'm sending to Anki-Connect

{
  "params": {
    "cardTemplates": [
      {
        "Name": "st listen",
        "Front": "<!-- Fields:Source ID,ChapterSection,Occurance,st,tst,Chapter,Section,sid -->\n<link href=\"./norwegian_template.css\" rel=\"stylesheet\" />\n\n<table class=\"front-card\">\n    <thead>\n        <tr>\n            <th>Skriv hva de sier . . .</th>\n        </tr>\n    </thead>\n    <tbody>\n        <tr>\n            <td>{type:st}</td>\n        </tr>\n        <tr>\n            <td>{sid}</td>\n        </tr>\n    </tbody>\n    <tfoot>\n        <tr>\n            <td>Kapittel {chapter}</td>\n        </tr>\n        <tr>\n            <td>Del {section}</td>\n        </tr>\n    </tfoot>\n</table>\n\n<script src=\"./norwegian_template.js\"></script>",
        "Back": "<!-- Fields:Source ID,ChapterSection,Occurance,st,tst,Chapter,Section,sid -->\n<link href=\"./norwegian_template.css\" rel=\"stylesheet\" />\n\n<table class=\"back-card\">\n    <thead>\n        <tr>\n            <th><div data-english=\"This is what they said . . .\">Dette er hva de sa . . .</div></th>\n        </tr>\n    </thead>\n    <tbody>\n        <tr>\n            <td class=\"big-text\">{sid} {type:st}</td>\n        </tr>\n        <tr>\n            <td><div data-english=\"{tst}\">oversettelse</div></td>\n        </tr>\n    </tbody>\n    <tfoot>\n        <tr>\n            <td><div data-english=\"Chapter {chapter}\">Kapittel {chapter}</div></td>\n        </tr>\n        <tr>\n            <td><div data-english=\"Section {section}\">Del {section}</div></td>\n        </tr>\n    </tfoot>\n</table>\n\n<script src=\"./norwegian_template.js\"></script>"
      }
    ],
    "modelName": "NN-SET Type 1",
    "inOrderFields": [
      "Source ID",
      "ChapterSection",
      "Occurance",
      "st",
      "tst",
      "Chapter",
      "Section",
      "sid"
    ],
    "css": " "
  },
  "action": "createModel",
  "version": 6
}

This is the error I get back

{
  "result": null,
  "error": "Card template ⁨1⁩ in notetype '⁨NN-SET Type 1⁩' has a problem.<br>See the preview for more information."
}

I'm not sure if this is an issue in the HTML templates or a something with Ank-Connect communicating with Anki? We can manually add this template and Anki accepts it without issue.

This is the HTML that is parsed out and added to the front/back of the card template being sent.

<table class="front-card">
    <thead>
        <tr>
            <th>Skriv hva de sier . . .</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>{{type:st}}</td>
        </tr>
        <tr>
            <td>{{sid}}</td>
        </tr>
    </tbody>
    <tfoot>
        <tr>
            <td>Kapittel {{chapter}}</td>
        </tr>
        <tr>
            <td>Del {{section}}</td>
        </tr>
    </tfoot>
</table>

<table class="back-card">
    <thead>
        <tr>
            <th><div data-english="This is what they said . . .">Dette er hva de sa . . .</div></th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td class="big-text">{{sid}} {{type:st}}</td>
        </tr>
        <tr>
            <td><div data-english="{{tst}}">oversettelse</div></td>
        </tr>
    </tbody>
    <tfoot>
        <tr>
            <td><div data-english="Chapter {{chapter}}">Kapittel {{chapter}}</div></td>
        </tr>
        <tr>
            <td><div data-english="Section {{section}}">Del {{section}}</div></td>
        </tr>
    </tfoot>
</table>
skint007 commented 8 months ago

The issue was with a conditional field that had the beginning tag but not the ending tag when trying to import/create. Also another issue that caused the same issue, trying to import a template with a cloze, but not specifying it is of cloze type. I've also noticed you get the same exact error if you try to use a field that doesn't exist in the field list, for example mistyping a fieldname :)