I have a collection named “Products”. It’s just a simple list of product names. I want to get the first text item in the first row (which is a product name) and display it in a text element.
Here’s the code I am starting with but it doesn’t work.
import wixData from ‘wix-data’;
$w.onReady( function () {
$w(‘#textSeriesName’).text = wixData.get(“Products”, “00001”)
});
Hi Sam: You need to familiarize yourself with Promises and how to use the wixData API. The get function is asynchronous and returns its result in a promise. So either you add a .then() after the get and assign the text value there or you use the async and await pattern.
Also the result you get will be an object. The object will contain key-value pairs that are mapped from the column names in your collection.
Check this out it might help:
So for example you code will probably work if you do something like this:
import wixData from 'wix-data';
$w.onReady(function () {
wixData.get("Products", "00001")
.then((productRecord) => {
$w('#textSeriesName').text = productRecord.productName;
});
});
This assumes that the first text item has a field key of “productName”