Working with Arrays

Hi, i am trying to have a button on my page, which would randomly access one of x number of items in an array. I got the script, but it doesn’t work the way i think it should, here are the issues:

1 . The script does access the array, but it sees only the last 50 added items to a data set. No matter how many items i have (if more than 50), it only sees the last 50.

  1. The button only works in the preview mode of my web. On the live web the browser doesn’t even recognize it as a button.

Here is the code:

import wixLocation from 'wix-location';
import wixData from 'wix-data';

export function button3_click(event) {
 let links = [];
    wixData.query('test').find().then((results) => {
            results.items.forEach((item) => {
 let link = "/test/" + item.title
                links.push(link)

 let linker = links[Math.floor(Math.random() * links.length)]
            wixLocation.to(linker);
        })
        .catch((error) => {
 let errorMsg = error.message;
 let code = error.code;
        });

}

Any thoughts are appreciated!

up

https://www.wix.com/code/home/forum/community-discussion/database-query-results-limited-to-50

Thank You!! This solved my first problem. Any ideas about the second one?

@kaikarisjonas What is the link to your site to test the button?

@mennyg19 here’s the link. The button is in the footer, named ‘Generate’.

https://kaikarisjonas.wixsite.com/random

@kaikarisjonas Thanks. Is it set to be enabled by default? It looks to me to be disabled.

@mennyg19 I’m not sure, will have to check that later, but i am wondering if there is an option to enable or disable button? I think it’s only wether it’s linked to a page or not.

@kaikarisjonas if you right click the element and click properties on the bottom of the drop-down menu, it will popup a small menu. One of the options in the menu is to be enabled or disabled by default.

@mennyg19 ok i’ll check it tomorrow. Thanks!

@mennyg19 I have checked it, it is enabled by default.

@kaikarisjonas in the html code of your page it appears as data-disabled:“true”. So check again (also you might disabled it somewhere in your code?)

+try log to your console the button status:

console.log($w("#button3").enabled);//if it's "false", it means the button is disabled and you should enable it;

@jonatandor35 If i link it to any page (i.e. Home page) the button works. My working process was simple: add the button, enable on-click function and write the code. Anything else i should have done?

@jonatandor35 The status is ‘ture’.

@kaikarisjonas so I don’t know…