Displaying user input, calculated value, and databse query on dynamic page.

I followed the directions here: https://support.wix.com/en/article/creating-an-expandable-text-box-with-a-show-more-link-using-wix-code
For creating an exandable textbox.
However; get an error " the element selector function (usually $w) cannot be used before the page is ready.

import wixData from ‘wix-data’;
let fullText; // variable to hold the full text
let shortText; // variable to hold the short version of the text

$w(“FormEnterRP”).onCurrentIndexChanged( function () { //<-- throws error .
// how many characters to include in the shortened version
const shortTextLength = 40;
// set the fullText variable to be the text from the collection
fullText = $w(‘#FormEnterRP’).getCurrentItem().begin; //<-- read current value in database
// if no text to display, collapse the text element and the button
if (!fullText) {
$w(‘#myTextElement’).collapse();
$w(‘#myButton’).collapse();
} else {
// if the text has fewer or the same number of characters as shortTextLength characters, display it as is and collapse the “Show More” button
if (fullText.length <= shortTextLength) {
$w(‘#myTextElement’).text = fullText;
$w(‘#myButton’).collapse();
} else {
// create the shortened version of the text and display it in the text element
shortText = fullText.substr(0, shortTextLength) + “…”;
$w(‘#myTextElement’).text = shortText;
}
}
});

export function myButton_mouseIn(event, $w) {
//Add your code for this event here:
// display the full text
$w(“#myTextElement”).text = fullText;
// collapse the button
$w(“#myButton”).collapse();
}

I am open to any suggestions for how to fill a dynamic page with output from user input, and from a database query I am open to suggestions.

I figured out my issues.
When you link a button to a database you cannot remove the connection completely.
Started over from scratch.
I have new pages, one dynamic. User can input two fields. begin and end. Code runs a calculation to find the difference of the two numbers.
This is output on a dynamic page.
I figured out my issues.
When you link a button to a database you cannot re move the connection completely.
Started over from scratch.
I have new pages, one dynamic. User can input two fields. begin and end. Code runs a calculation to find the difference of the two numbers.

Now left to do is query a new database with the difference value
Example: database named permuations with column 1 named RP and column 2 the count of each value in column 1. Essentially it is 17P7 + 17P6 +17P5 +17P4 +17P3 +17P2 +17P71, with the values 1 though 17. Then each permutation was summed .Removing duplicates this is column 1. Column 2 is how many times each sum occurred in the original permuation.
So> Query this database in column 1 and return the value in the same row in column 2.

Next query uses the same difference value and looks for matches in a different database.
This database has column 1, the sums of the permutation. and column 2 the associated unique permutation for each sum.

Example:
Database 1:
Col1 Col 2
7 1
8 1

13 2

database 2:
Col 1 Col 2
7 1 1 1 1 1 1 1 ← note: col 2 here is replaced with text.
8 1 1 1 1 1 1 2
13 1 1 1 1 1 1 9
13 1 1 1 1 1 2 8

Help with how to build these queries and submit the results to a textbox would be appreciated.

Note: the permutation follows this structure. Order does not matter and repetition is allowed. This explains the first two rows in database 2.