if/else statement works weird in published site but works normally in preview mode?

Hi all! I’m having this problem that’s been bugging me for a whole week.

Apparently, when I run my page’s code in preview mode, it works just fine, there’s nothing wrong with it. But when I run it in the published site, the if/else statement doesn’t work. In fact, they both execute at the same time! (eg. if a = 1, do this, else do that, in my published site, it does both!!!)

If my code doesn’t have any .update(), then everything’s fine. But as soon as I insert an .update() function, it just goes wild…

Any help would be appreciated!

Hi, Shiro Katake .

  1. What is the if/else statement in question? (Paste the code in a reply to this message.)

  2. “it just goes wild.” is not particularly descriptive; is there a particular behavior that occurs (e.g., text displayed on the monitor, etc.)?

Ah yes, sorry about that @abergquist . Here’s the part of the code that was causing problems. I apologize in advance for displaying so much code…I don’t know where the problem is occurring so I was afraid of showing my code. But now that you’ve asked, I’ll try my best to be concise with what I’m showing (I’ll be showing the part of the base code that’s acting “weird” and all the functions that that part of the base code is using).

For context: I’m designing a webapp for a pervasive game (outdoor game if that makes more sense) that involves players scanning QR codes from one another. One side is the virus and the other side is the white blood cell. Every time the user scans a player, the code would compare the 2 players’ types.

  • If they’re of different types, then it would proceed and grant score + update the other player’s type to their type. (eg. If a virus scans a white blood cell, the white blood cell would become a virus, and vice versa)
  • If they’re of the same type, then the code would redirect the user to the “about” page.

This is the “base” code:

$w.onReady(async function () {
     let query = wixLocation.query;
     //If user is not logged in, redirect to home page, if not proceed
     if (!wixUsers.currentUser.loggedIn || query === undefined) {
         wixLocation.to("https://www.infectiogames.com");
     } else {
         //Get user's id and query value
         //All queries will look like this: ?q=type_uniquecode
         //type can be in 2 forms: "users" or something else
         let userId = wixUsers.currentUser.id;
         let querySplitted = query.p.split("_");
         let uniqueCode = querySplitted.splice(1, 1).toString();
         let type = querySplitted.splice(0, 1).toString();
         console.log("type: " + type);
         console.log("uniqueCode: " + uniqueCode);

         //If the query has "users" being the type, then 
         if (type === "users") {
         
             //Compare user's type and enemy's type
             //If their types are the same, redirect the user to "about"
             //This is where the problem starts to occur
             //Let's say we have userType being "Virus" and enemyType being "White cell"
             
             let userType = await GetUserType(userId);
             let enemyType = await GetEnemyType(uniqueCode);
             console.log("userType: " + userType);    //This would be "Virus", which is expected
             console.log("enemyType: " + enemyType);  //This would also be "Virus" instead of "White cell"
             console.log(userType === enemyType);     //This would be true....instead of false
             if (userType === enemyType) {
                 console.log("redirecting to about"); //THIS WOULD RUN
                 //wixLocation.to("https://www.infectiogames.com/about")
             } else {
                 //If not, perform KillConfirmed
                 KillConfirmed(userId, uniqueCode);   //THIS WOULD ALSO RUN
             }
         } else {
             //More code
         }

Below are the functions that the code is using:

function KillConfirmed(userId, enemyId) {
    //Find user's data in the database
    wixData.query("users")
    .eq("u_uid", userId)
    .find()
    .then((results) => 
        let item = results.items[0];
        console.log("user from KillConfirmed " + userId + ": ");
        console.log(item.u_type);
        
        //If user's type is virus, then add one to white cell kill count, or vice versa, or display error
         switch (item.u_type) {
         case "Virus":
             console.log("KillConfirmed user type: " + item.u_type);
             item.u_wKills += 1;
             break;
         case "White cell":
             console.log("KillConfirmed user type: " + item.u_type);
             item.u_vKills += 1;
             break;
         default:
             console.log("Error: Unknown user type");
             break;
         }
        item.u_score += 100;
        console.log("u_score: " + item.u_score);
        
        //If I turn this .update().then() code into a comment, everything's fine
        wixData.update("users", item)
        .then(() => {
            console.log("Update finished"); 
            //Update enemy's type after being killed 
            UpdateEnemyType(item.u_type, enemyId);
        })

        console.log("Kill recorded.");
    });
}


function UpdateEnemyType(userType, enemyId) {   
    wixData.query("users")
    .eq("u_uid", enemyId)
    .find()
    .then((results) => {
        let item = results.items[0];
        console.log("user from UpdateEnemyType " + enemyId + ": ");
        console.log(item.u_type);
        switch (userType) {
        case "Virus":
            item.u_type = "Virus";
            break;
        case "White cell":
            item.u_type = "White cell";
            break;
        default:
            console.log("Error: Unknown user's type");
            break;
        }
        wixData.update("users", item);
    });
}
function GetUserType(userId) {
    return wixData.query("users")
    .eq("u_uid", userId)
    .find()
    .then((results) => {
        return results.items[0].u_type;
    })
}
function GetEnemyType(enemyId) {
    return wixData.query("users")
    .eq("u_uid", enemyId)
    .find()
    .then((results) => {
        return results.items[0].u_type;
    })
}

An interesting thing is, if I don’t perform any .update() functions, then everything will be just fine (except nothing will happen, the database won’t be updated, but the console,logs() would display the correct values). But as soon as I type in an .update() function. That (the problem that I described in the base code) happens.

Again I apologize for displaying so much code…

If the code works in preview mode but not in the published site, I believe I have to defer to Corvid/Wix support to explain why.

Question: does the update() call never work at all or does it only work in preview mode?

BTW, cool idea for a website/web game!

A couple of grammatical opportunities for you: on both the Home and About pages, “health and virus free” needs to be replaced with “healthy and virus-free” in the following sentence:

“Inside the body of Swinburne, roughly 60.9 thousand cells are working hard every day, keeping the body health and virus free.”

Thanks @abergquist , I’ll fix that one up! Everything works (kind of) normal when I do the .update() call. If the players’ types are different, the database is updated AND they’ll be redirected to about (because apparently both players’ types are the same now, even though this happens AFTER that if/else statement…). If the players’ types are the same, then nothing will happen to the score, and the user will be redirected to about.

So “mechanic” wise, everything’s fine. Nothing can be exploited from this bug. The only thing that’ll be annoying to players is eventually I’ll turn that About page-redirection to a page that’ll say “You can’t scan players of the same type”, and there’ll be confusions going around.