Display calculated number in a text field

Hello,

I am new to wix coding and pretty lost with a rather basic problem:

After user input of a number, I calculated different numbers from the input number using a hook (afterQuery). All numbers (input + calculated) are stored in the same database. Now I wanted to display one of the calculated numbers of the first item of that database.
I used all functions from this wix article:

my afterQuery hook (save in a data.js file) looks like this:

export function Rate_afterQuery(item, context) {
//TODO: write your code here…
item.rateBasic = 0.01
item.ratePremium = 0.02
item.priceBasic = item.wordNumber * item.rateBasic;
item.pricePremium = item.wordNumber * item.ratePremium;

return item;
}

and my onReady function looks like this:

$w.onReady( function () {
$w(“#dataset1”).onReady(() => {
populateCalculatedFields();
} );
} );

function populateCalculatedFields() {
const currentItem = $w(“#dataset1”).getCurrentItem();
$w(“#text40”).text = currentItem.priceBasic;
}

In the preview, I receive the following error message:
Wix code SDK Warning: The text parameter of “text40” that is passed to the text method cannot be set to null or undefined.

The afterQuery hook seems to work, as all values in the database are calculated correctly. When I change the last line in the populateCalculatedFields function to the following one, the error above disappears and the word “hallo” is displayed in the respective text field.
$w(“#text40”).text = “hallo”

…so there is definitely a problem with the field type, could it be solved by converting the variable “currentItem.priceBasic” into a string? How would I do this correctly? How would I assign this string correctly to $w(“#text40”).text? Is there maybe a completely different problem?

Thank you so much for your help, I am fighting for a long time with this probably simple problem…
NH

Hey
What do you get if you console log the currentItem? console.log(currentItem); Make sure you have data in the field you are trying to use.

Then if that field is a number you need to convert it to a string using currentItem.priceBasic.toString(); to make it work as a value in a text field.

Thank you a lot for your answer! I tried this and apparently the content seems to be undefined, so there needs to be the problem!

I tried to display the data now in a table by only connecting the table to the respective columns of my dataset. After input in the preview, the number I typed in is displayed in this table, but after this number appears, an empty line is added as the first line of the table. Does anybody know why this might happen? In the corresponding dataset no additional empty line is added. However, if my onReady function defines this additional new empty line as currentItem, this might explain that the content I wish to display is empty…
Does anybody have any idea how to solve this issue?

Thank you so much for your help!

Go into your data collection and delete that empty line. It usually adds a new line if you enter records using keyboard and TAB keys. So if you get an empty line in your code there is a empty line inside your data collection.

mmh…I also thought so, but actually I do not see any additional line in my dataset. However, there is clearly an extra line added as the first line to table in the preview. Maybe this is not even the basic problem it it is not added to the dataset? However I wonder why it is added.

And if this is not the problem, why is the data (that I clearly see in my dataset) not displayed?

Some Update to my problem:

apparently the main problem seems to be that the data I wish to display are “null or undefined”. As the database is definitely not empty, I tried to display elements of this database in the regular manner (only connecting the text element with the data and not using the afterQuery hook). This works great. As also the afterQuery hook works great (after form submission, the input data appears in the database and all numbers have been calculated from the input number, as defined in the afterQuery hook), the problem needs to be in my onReady function:

$w.onReady( function () {
//TODO: write your page related code here…

$w(“#dataset1”).onReady(() => {
populateCalculatedFields();
} );
} );

function populateCalculatedFields() {
const currentItem = $w(“#dataset1”).getCurrentItem();

$w(“#text40”).text = currentItem.priceBasic.toString();
console.log(currentItem)
}

Checking wix articles, I found the following article, that the problem might be the getCurrentItem() function:

The article says, the getCurrentItem() function returns “null or undefined” if the dataset is empty (this is not the case), is filtered (this is also not the case), or has not loaded yet. As the first to reasons can be excluded, the problem needs to be that the dataset has not loaded yet. I just wonder, why? I already applied the wix suggestion to put the dataset’s onReady function into the page’s onReady event handler. Furthermore, I waited a long time for loading. Both did not solve the problem. Furthermore, as soon as I go the preview, the following error appears:

An error occurred in one of datasetReady callbacks TypeError: Cannot read property ‘toString’ of undefined

Indicating, that directly “undefined” is returned as soon as the preview is opened.

Can somebody help me with this problem? Maybe also someone from the wix team? That would really help a lot…

Thank you so much!
NH