Querying works in Preview, not Publish

import wixData from 'wix-data';

function getData() {
 let query = wixData.query('Collection');
 return query.limit(100).find().then( (results) => {
        console.log(results);
 return results.items;
    })
    .catch( (err) => {
 let errorMsg = err;
    });
}

$w.onReady(function () {
 //TODO: write your page related code here...
    $w('#dataset1').onAfterSave( () => {
        getData().then((items) => {
            console.log(items);
 if (items !== undefined){
 var months = 0;
                months = (items[0]["goal"] - items[0]["accountBalance"]) / (items[0]["gain"] - items[0]["loss"]);
 var days = 0;
                days = Math.floor(months * 30);
 if (days > 0) {
                    console.log(months);
                    console.log(days);
                    $w('#text43').text = days + " Days";
                } else {
                    $w('#text43').text = "You got to your goal!"
                }
            }
        })
        .catch( (err) => {
 let errorMsg = err;
            console.log(errorMsg);
        });
    }); 
});

When i open it in preview, it calculates the number of days correctly to display. In publish, i get undefined for my query. Anyone can help?

My “#dataset1” is set to write only and the text element that I want to display is attached to my “#dataset2” with read only.

Have you checked your database permissions? Is it set to member or anyone, etc?
Have you synced your information to live database? Or is live database blank?

Thanks for the reply! I had to use custom database permissions and set everything to anyone, didn’t know that form submission was only for collecting.