Code runs in Preview but not Live

I’m creating a website for a friend’s softball league (currently: https://larrystunts.wixsite.com/stuntsoftballleague). I want to create a page that shows the team standings. There are three database’s in play: Games, TeamWinLossTie, TeamStandings. The first one records game information, most importantly each record has HomeTeam, HomeScore, AwayTeam, AwayScore. The second is a “calculated” intermediary to allow me to allocate a win, loss or tie for each game for each team (since the team can be the home team or away team this isn’t in one column). The third database is also “calculated”. It is the source for the page table. In it is the sum of wins, losses and ties by team and a calculated win percent.

With the help of this community (and Wix Reference) I’ve figured out async/away, arrays (for performance), aggregate, etc. It all works perfectly in Preview but NOTHING runs in Live. I know this because the first step in the process is to truncate the second and third databases and even that one simple command doesn’t happen.

I understand Sandbox versus Live data and I’ve synced the Games database (that’s the only one that needs to start with data - remember the first couple lines should truncate the other databases anyway.) I’ve set all the databases to have custom permissions with ANYONE having all access rights. (Probably not necessary for the Games table since that is only read.) And I’ve set all three database connectors to Read & Write mode (again probably not necessary, at least for the Games databases.)

As I said, everything runs perfectly in Preview but not in the Live site. Help would be appreciated!

Thanks,
Bruce

Here’s the Pagecode (#dataset1 = TeamWinLossTie, #dataset2 = Games, #dataset3 = TeamStandings):

import wixData from ‘wix-data’ ;
import wixWindow from ‘wix-window’ ;
$w.onReady( async function ()
{
let trunTeamWinLossTie = await wixData.truncate( “TeamWinLossTie” );
let trunTeamStandings = await wixData.truncate( “TeamStandings” );

var allScores=;

//Begin populate TeamWinLossTie
const totalGames = $w( “#dataset2” ).getTotalCount();
let result = await $w( “#dataset2” ).getItems( 0 , totalGames);
let allItems = result.items;
for ( let i= 0 ; i < result.totalCount ; i++)
{
let hometeam=allItems[i].homeTeam;
let homescore=allItems[i].homeTeamScore;
let awayteam=allItems[i].awayTeam;
let awayscore=allItems[i].awayTeamScore;
if (homescore == null )
{
}
else if (homescore>awayscore)
{

              allScores[i* 2 ]={team: hometeam, win: 1 , loss: 0 , tie: 0 } 
              allScores[i* 2 + 1 ]={team: awayteam, win: 0 , loss: 1 , tie: 0 } 
          } 

else if (homescore<awayscore)
{
allScores[i* 2 ]={team: hometeam, win: 0 , loss: 1 , tie: 0 }
allScores[i* 2 + 1 ]={team: awayteam, win: 1 , loss: 0 , tie: 0 }
}
else if (homescore===awayscore)
{
allScores[i* 2 ]={team: hometeam, win: 0 , loss: 0 , tie: 1 }
allScores[i* 2 + 1 ]={team: awayteam, win: 0 , loss: 0 , tie: 1 }
}
}
await wixData.bulkInsert( “TeamWinLossTie” ,allScores);

//End populateTeamWinLossTie

//Begin populateTeamStandings

var allStandings=;
let aggResults = await wixData.aggregate( “TeamWinLossTie” )
.group( “team” )
.sum( “win” )
.sum( “loss” )
.sum( “tie” )
.run();
let sumItems = aggResults.items;

for ( let i= 0 ; i < aggResults.items.length ; i++)
{
var tN = sumItems[ 0 ,i].team;
var w = sumItems[ 0 ,i].winSum;
var l = sumItems[ 0 ,i].lossSum;
var t = sumItems[ 0 ,i].tieSum;
var tpoints = t/ 2 ;
var wP = (w+tpoints)/(w+l+t);

      allStandings[i]= {teamName: tN, wins: w, losses: l, ties: t, winPercent: wP}; 

  } 

await wixData.bulkInsert( “TeamStandings” ,allStandings);

//End populateTeamStandings*************************

// Added just in case the page paints before the data in #dataset3 (TeamStandings) is populated
$w( “#dataset3” ).refresh();
$w( “#table3” ).refresh();

//End *************************

});

Have you published your site? Your code will only run in preview mode unless you publish your site & reload your live site to see the changes.

I sure did.

This might be a problem on wix side as they are making updates. Please contact customer support. I don’t think this is a corvid related issue.
customer support: support@wix.com

Hello together,

did you already have tried to Log-In as ADMIN with full rights and test it ?
If not yet, then try this first.

And i recognized that you do not use the onReady-command for datasets.

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


  });
});

You have a point.

I suspect that it has to do with permissions. As stated in the truncate() API:

truncate() runs when at least one of the following is true:

  • The current user is the site owner.

  • The request is performed in backend code with a suppressAuth options value of true.

This means that truncate will not work for any anonymous site visitor since they are not the site owner. This even includes you if you are not logged in as the site owner.

The way you can get around this is to move your database code into the backend, and use the suppressAuth option.


However, I have to admit that I don’t understand why you are clearing and populating a standings collection each time the standings are viewed. If more than one user is viewing this at one time it will cause a conflict - who clears? who populates the collection?

I would suggest that all you need to do is to calculate the standings as you already are doing, and then use the calculated results to populate the table. No reason to clear, calculate, and populate a special standings collection for each view of the team standings.

Thank you russian-dima.

I want any site visitor to be able to run this process so logging on as a site ADMIN won’t help.

Also, I just learned this coding in the last week or two. I’m not sure where or why I’d use the on-ready command for datasets. The only dataset that has data is #dataset2. The other two are being written with async/await.

Maybe I just don’t understand…

Thanks Yisrael.

I’ve suspected permissions too. But I don’t know what or where. You pointed out the truncate command might be the culprit (thanks again!) but if I remove that command it still doesn’t run. Is there another command in my code that would require ADMIN permissions?

I think the logic behind the code is pretty sound. I can only assume that there is data in the games database and it may change at any time, hence the standings have to be able to change at any time too. That’s why i need to calculate and populate the standings database every time. You’re right that there would be a problem if 2 (or more people) went to the page within 2 seconds of each other (that’s how long it takes for the code to run) but that’s highly unlikely. I guess an alternative might be to populate the standings on some event associated with the games database being updated but I’d still have to execute this same code that doesn’t work.

Also, I don’t know if this is right, but I added the following code to the very beginning. It ran without errors but still nothing happened - it didn’t populate the TeamWinLossTie or TeamStandings databases.

I’d really appreciate any other ideas you might have for me.

I’m trying to post the code I added but this board won’t let me… it thinks I’m trying to post a link - but I’m not!!

import wixUsers from ‘wix-users’ ; //added before the page function

let email = "site-owner-email-address " ;
let password = "site-owner-password " ;
await wixUsers.login(email, password);

The best way to display the standings would be to do the calculations - that you are already doing anyhow - and then display them directly in a table without using a database collection. This would be faster, wouldn’t matter who views the standings, wouldn’t matter how many visitors are viewing the site, and using the collection to set the table contents is just an unnecessary step.

You were trying to post “gmail.com” which is a link.

For accounts that are not yet authorized to post links, you can do something likee this: gmail .com

Like always, very good explanation Yisrael. Have learned new things.
THX.

I agree with your suggestion that a better approach is to do away with those intermediary databases. I thought about that too but not being a programmer, I struggled to figure out how to do the aggregation that I could easily do with that aggregate function against a database.

Any ideas or suggestions on how to do that would be helpful. (I’ll probably have to do some looping with arrays. Right?)

Thanks again for your advice and support!

If you want to be technically precise, I said " it thinks I’m trying to post a link ". I may have been posting a link but I absolutely was not “trying to post a link.”