Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mikkel1156 opened this issue Apr 3, 2023 · 2 comments
Closed

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

mikkel1156 opened this issue Apr 3, 2023 · 2 comments

Comments

@mikkel1156
Copy link

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
Copy link
Owner

zadam commented Apr 3, 2023

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
Copy link
Author

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants