Null value from query

Here’s my code:

$w.onReady(function () {

let randomItem; 
let randTitle; 

wixData.query("RandomLinks") 

.find()
.then( (results) => {
let randomNumber = Math.floor((Math.random() * results.items.length));
randomItem = results.items[randomNumber];
randTitle = randomItem.title;
});

$w('#button13').label = randTitle; 

});

The problem is I keep getting this error:

“Wix code SDK Warning: The label parameter of “button13” that is passed to the label method cannot be set to null or undefined.”

Real quick: Yes, title is the field key and not the name of the field, so that’s not it! Have tried all kinds of hacks, but cannot get that random title to take. Any ideas?

The error means that your randomItem.title field is empty or invalid. You might want to retrace your steps and use a line by line code and test approach to catch where you went wrong.

I would also debug this using console.log() to check the results array.

If the array is good then console log the next 2 lines.

You need to move this line inside the .then() function:

$w('#button13').label = randTitle;

Aaaaaand it was a scope problem! Yisrael, I owe you a coke!