zadam / trilium

Build your personal knowledge base with Trilium Notes
GNU Affero General Public License v3.0
27.2k stars 1.9k forks source link

(Bug report) New type widget gets content as undefined #3795

Closed mikkel1156 closed 1 year ago

mikkel1156 commented 1 year ago

Trilium Version

0.59.2

What operating system are you using?

Other Linux

What is your setup?

Server access only

Operating System Version

Linux pyrite 5.15.82 #1-NixOS SMP Thu Dec 8 10:28:45 UTC 2022 x86_64 GNU/Linux

Description

I'm in the process of creating a new type widget, and I want to save the settings to the notes content instead of using attributes like I am currently. However when I try request the content in various ways (looking at other code) it returns as undefined.

This is the relevant code and some commented out stuff I've used in testing:

import server from "../../services/server.js";
import froca from "../../services/froca.js";
import noteCreateService from "../../services/note_create.js";
import attributesService from "../../services/attributes.js";
import TypeWidget from "./type_widget.js";

const TPL = `
<div class="note-detail-data-table note-detail-printable">
    <style>
        .data-table-content {
            margin-top: 50px;
        }

        .data-table-content button {
            width: 50%;
            font-size: 1.25em;
            margin: 0% 10%;
        }

        .data-table-content input {
            width: 100%;
            border: none;
        }

        .data-table-content input:focus-visible {
            outline: none;
        }

        .data-table-content table th {
            text-transform: capitalize;
        }
    </style>
    <div class="data-table-content"></div>
</div>`;

export default class DataTableTypeWidget extends TypeWidget {
    static getType() { return "dataTable"; }

    doRender() {
        this.$widget = $(TPL);
        this.$content = this.$widget.find(".data-table-content");

        super.doRender();
    }

    async getData() {
        return {
            content: JSON.stringify({})
        };
    }

    async getContent(note) {
        let resp = await server.post("sql/execute", { query: `SELECT content FROM note_contents WHERE noteID='${note.noteId}';` } ,"data_table");
        // console.log("DEBUG CONTENT", resp, resp.results[0][0].content.data.toString());
        console.log("DEBUG CONTENT", String.fromCharCode(...resp.results[0].content.data));
    }

    async saveData() {
        let resp = await server.put("notes/"+note.noteId+"/data", { content: JSON.stringify(this.state) }, "data_table");
    }

    async doRefresh(note) {
        this.$widget.show();
        // const noteComplement = await this.noteContext.getNoteComplement();
        // console.log("DEBUG", await note.getContent());

        // Quick way to set content in testing.
        // let resp_new = await server.put("notes/"+note.noteId+"/data", { content: "DEBUGGING CONTENT STUFF" }, "data_table");
        // let resp_new = await server.get("notes/"+note.noteId, "data_table");
        // let resp_new = await froca.getNote(note.noteId);
        const noteComplement = await froca.getNoteComplement(note.noteId);
        console.log("UPDATE", noteComplement);

        const noteId = noteComplement.noteId;
        console.log("NOTE", note);

        await this.getContent(note);

        this.$content.show().scrollTop(0);
    }
}

In the uncommented parts I'm basically just getting the note complement, and I get everything except the content. image

Similarly trying to use await note.getContent() gives the same. I've only managed to get the content by executing SQL queries (using a custom endpoint I made for quick testing). But I want to use the builtin features to get the content as expected. So feels like I'm missing something.

Using node v18.12.1 if that matters.

Error logs

No response

zadam commented 1 year ago

await froca.getNoteComplement(note.noteId); should generally work, not sure what's wrong.

Might be perhaps because it's not recognized as text data? I think binary is never returned to frontend in this form.

mikkel1156 commented 1 year ago

await froca.getNoteComplement(note.noteId); should generally work, not sure what's wrong.

Might be perhaps because it's not recognized as text data? I think binary is never returned to frontend in this form.

You were totally correct. I didn't realize that the mime is used in that way, so since it set it as empty (copied book code) it was just giving me binary.

If we want to note these things down for the future of people who want to contribute, is there a good place for that?