Limit text length in a database repeater

I’m currently struggling to limit the number of characters displayed in a text box on a database connected repeater, I can’t get it working… This is my code, any ideas?
$w.onReady( function () {
$w(" #eventphotos “).onReady(() => {
let text = $w(” #eventphotos “).getCurrentItem().unsearchedrepdescrip;
if (text.length > 100) {
text = text.substring(0,97) + ‘…’; }
$w(” #unsearchedrepdescrip ").text = text;
});
})
Thank you so much!

@tomer-wix

$w.onReady( function () {

    $w("#eventphotos").onReady(() => { 

//change repeater1 to the ID of your repeater
$w(“#repeater1”).forEachItem(($w) => {

let text = $w(“#eventphotos”).getCurrentItem().unsearchedrepdescrip;

//now we make text short, modify length as required
let shortenedtext = text.slice(0, 10);

if (text.length > 100) {

//unsearchedrepdescrip is the ID of the text on your repeater, change ID as necessary
$w(“#unsearchedrepdescrip”).text = shortenedtext;

            } 
        }) 

}) 

})

Thank you!! It’s throwing me this error though…

i have temporally removed the shortened text, what value does it log for “text”

$w.onReady( function () {

    $w("#eventphotos").onReady(() => { 

//change repeater1 to the ID of your repeater
$w(“#repeater1”).forEachItem(($w) => {

let text = $w(“#eventphotos”).getCurrentItem().unsearchedrepdescrip;

            console.log(text); 

//now we make text short, modify length as required
//let shortenedtext = text.slice(0, 10);

if (text.length > 100) {

//unsearchedrepdescrip is the ID of the text on your repeater, change ID as necessary
// $w(“#unsearchedrepdescrip”).text = shortenedtext;

            } 
        }) 

}) 

})

if it give you a value of undefined it’s probably because you have not written the correct field Id for the column in your collection when you are getting current item, are you sure unsearchedrepdescrip is the name of the field in your collection ?

@mikemoynihan99 It’s logging it as undefined… I also noticed that it’s logging my description field as null even though it shows up in the repeater, not sure if that means anything…

[wix-dataset.getCurrentItem] returned with ( {“_id”:“4997649b-20a5-4681-a693-bd2903ba82d3”,“_owner”:“71a24938-8ae7-446a-bb81-bcbb0f12022a”,“_createdDate”:“2018-12-11T23:40:52.672Z”,“_updatedDate”:“2018-12-11T23:42:54.527Z”,“relatedjob”:“OHSU Knight Cancer Ribbon Cutting”,“eventDate”:“2018-09-10T08:00:00.000Z”, “jobdescription”:null,

@mikemoynihan99 Ah shoot I was using ID of the repeater element, not the field key of my actual database. I’ll go try it that way, but I’m pretty sure I tried a variation of it that way and it didn’t work… One sec

Now it’s logging as ‘null’ and showing this error : An error occurred in one of datasetReady callbacks TypeError: Cannot read property ‘length’ of null

The field I’m trying to shorten is a rich text field, if that helps. This is how I got it to work for a repeater showing database query results…

$w.onReady( function () {
$w(“#searchedrep”).onItemReady(($item, itemData, index) => {
$item(“event”).text = itemData.relatedjob;
$item(“#description”).html = itemData.jobdescription.substring(0, 225) + ‘…’;
$item(“#thumbnail”).src = itemData.thumbnail;
$item(“#thumbnail”).link = itemData[‘link-EventPhotos-relatedjob’];
$w(“#thumbnail”).clickAction = “link”;
})
});

@elena
have you posted the wrong code, this looks nothing like your 1st post ?

@mikemoynihan99 No, I was just trying to show you how I was able to make it work for a repeater that’s showing query results from my database in case that helped illuminate my issue somehow. The code in my first post is still what I’m working on!

This is what I have currently:

w.onReady( function () {

    $w("#eventphotos").onReady(() => { 

//change repeater1 to the ID of your repeater
$w(“#unsearchedrep”).forEachItem(($w) => {

let text = $w(“#eventphotos”).getCurrentItem().jobdescription;

            console.log(text); 

//now we make text short, modify length as required
let shortenedtext = text.slice(0, 10);

if (text.length > 100) {

//unsearchedrepdescrip is the ID of the text on your repeater, change ID as necessary
$w(“#unsearchedrepdescrip”).text= shortenedtext;

            } 
        }) 

}) 

})

@elena

null would suggest that there is no data in that field key

Well, there are some database entries that don’t have data in that field, is there a way to circumvent that?

Ah wonderful! I just added a “test” word to each database entry that didn’t have anything in that field and it worked, but there will be entries without any data, can I add some code that says something like “if this is null, then don’t shorten it” ?

@elena
try this

$w.onReady( function () {

    $w("#eventphotos").onReady(() => { 

//unsearchedrep to the ID of your repeater, change as necessary
$w(“#unsearchedrep”).forEachItem(($w) => {

if ($w(“#eventphotos”).getCurrentItem().jobdescription !== null ){

let text = $w(“#eventphotos”).getCurrentItem().jobdescription;

            console.log(text); 

//now we make text short, modify length as required
let shortenedtext = text.slice(0, 10);

if (text.length > 100) {

//unsearchedrepdescrip is the ID of the text on your repeater, change ID as necessary
$w(“#unsearchedrepdescrip”).text = shortenedtext;

            } 
            } 
        }) 

}) 

})

@mikemoynihan99

It’s still showing me the undefined error…

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

@elena
try this

$w.onReady( function () {

    $w("#eventphotos").onReady(() => { 

//unsearchedrep to the ID of your repeater, change as necessary
$w(“#unsearchedrep”).forEachItem(($w) => {

let text = $w(“#eventphotos”).getCurrentItem().jobdescription;

            console.log(text); 

if (text !== null ){

//now we make text short, modify length as required
let shortenedtext = text.slice(0, 10);

if (text.length > 100) {

//unsearchedrepdescrip is the ID of the text on your repeater, change ID as necessary
$w(“#unsearchedrepdescrip”).text = shortenedtext;

            } 
            } 
        }) 

}) 

})

No, it’s still giving me the error, unfortunately…