Hello everyone, please help to understand what is going on with my code.
i dont understand what i have to do to get the value of an item and do whatever with that value in a Dataset Collection. Here is my code:
import wixData from 'wix-data';
import wixWindow from 'wix-window';
var a;
$w.onReady( () => {
$w("#dataset2").onReady( () => {
a=$w("#dataset2").getItems(1, 7)
.then( (result) => {
let items = result.items;
let totalCount = result.totalCount;
let offset = result.offset;
console.log(items)
} )
.catch( (err) => {
let errMsg = err.message;
let errCode = err.code;
} );
} );
$w("#text17").text=String(a)
} );
I’m trying here, different things, but no one works.
1- Put the value of the DataSet items in the var a
2-Change the value of the “text17” to the value of the var a
3- log into the console the items
Please help, what i have to do to get the value of the dataset collection. I leave also a picture of the dataset #2.
Thanks
Hi.
Can see that you are assigning data to the text outside the code block. To display a single field of an item you can put your text element inside the code as shown below:
$w.onReady(() => {
$w("#dataset3").onReady(() => {
a = $w("#dataset3").getItems(1, 7)
.then((result) => {
let items = result.items;
let totalCount = result.totalCount;
let offset = result.offset;
$w("#text17").text = String(items[0].title);
console.log(items, totalCount)
})
.catch((err) => {
let errMsg = err.message;
let errCode = err.code;
});
});
//$w("#text1").text = String(a)
});
For all the rest of the data consider using the forEachItem() method of the repeater.
Good luck!
FIrst of all, Thank you so much Sam Eru. I think i’m having problems understanding how does the items works. I changed this line code:
$w("#dataset2").getItems(1, 7)
to:
a=$w("#dataset2").getItems(0, 15)
and i get a result, but when i change to a 1 or other number it tells me: “Cannot read property ‘title’ of undefined” same happen when i mainten the function getItems as (0,15) and i change the line:
$w("#text17").text = String(items[0].title);
to:
$w("#text17").text = String(items[1].title);
Could you help me understanding this or, just tell me where can i find some documentation to understand these indexes
Thanks!
Hello.
“Cannot read property ‘title’ of undefined” error message means that the database collection field you have called in your code is empty, that is, no item has been added to this database collection field. You can go to the database and check your collection fields.
Good luck!
import wixData from ‘wix-data’
$w.onReady( () => {
$w( “#myDataset” ).onReady( () => {
$w( “#myDataset” ).getItems( 0 , 1 )
.then( (result) => {
let items = result.items;
let totalCount = result.totalCount;
let offset = result.offset;
$w( ‘#yes’ ).text = items[ 0 ][ “yes” ].toString()
} )
. catch ( (err) => {
let errMsg = err.message;
let errCode = err.code;
} );
the given code i wrote before the example of the reaching specific field on database.
good luck