Page Code working in Preview but not Published?

Hi all,

II’m having an issue where everything in my page works fine in preview mode but as soon as I publish it, it doesn’t work.

A bit of background:
My code takes an input and basically matches that input with a URL from a certain carrier (url is determined by first 3 digits of the input), puts both the URL and input into a data set on change, and when the user presses the search button the page redirects to a “tracking redirect” page that concatenates the two inputs and goes to that URL.

Problem:
This all works in preview. I debug by console.log-ing the key variables which all show up fine in preview. When I publish it, the URL that’s supposed to be matched returns as “undefined” instead of what it should be.

Why would this happen? Shouldn’t preview mode be an accurate representation of the site’s behavior before actual launch?

My code is below:

import wixData from 'wix-data';

$w.onReady(function () {
    $w('#AWB').show('fly')
    $w('#searchawb1').show('fade')
});

export async function AWB_change(event) {
    $w('#text35').hide('fade')
    
    //user input
    let AWB = $w('#AWB').value
    
    //to determine airline
    let prefix = AWB.slice(0,3)
    
    //to append url with
    let suffix = AWB.slice(3)
    
    let info = await query_airline(prefix)
    let carrier = info[0]
    $w('#text36').text = "Searching " + carrier
    let i = info[1]
    let baseurl = await get_url(i)
    
    // this is where the issue is: working in preview but not published
    console.log(baseurl)
    
    //saves reference and url to dataset that pulls in next redirect page
    await save_dataset(suffix, baseurl)
    }
    
function query_airline(prefix){
    return wixData.query('Airlines')
        .contains('awbPrefix', prefix)
        .find()
        .then( (results) => {
    if(results.items.length > 0) {
    let items = results.items;
    let info = [items[0].airline, parseInt(items[0].index,10)]
    return info
    } else {
    $w('#text35').show('fly')
    }
    } )
    .catch( (error) => {
let errorMsg = error.message;
let code = error.code;
} )
}

function get_url(i){
let url_array = [
'https://www.ups.com/actrack/track/submit?awbNum=406',
'https://freight.qantas.com/online-tracking.html?airWaybills=081-',
'https://www.cathaypacificcargo.com/ManageYourShipment/TrackYourShipment/tabid/108/SingleAWBNo/160-',
'https://www.airnewzealand.co.uk/international-cargo-track-and-trace',
'https://www.brcargo.com/ec_web/Default.aspx?Parm2=191&Parm3=?TNT_FLAG=Y&AWB_CODE=695&MAWB_NUMBER='
]
return url_array[i]
}

async function save_dataset(suffix, baseurl){
$w("#dataset1").setFieldValue("reference", suffix)
$w("#dataset1").setFieldValue("url", baseurl);
$w("#dataset1").save();
}

ANY suggestions would help.
Thank you!!!

Hey
First guess I’d suggest is to end your lines. See several spots that seem to not have a ;
in the preview several code mistakes can be handled and run anyway. but on published its limited by what the browser will be able to handle.

Also does the databases exist on the published site and not just as a sandbox.
Hope this helps
Best regards
Claes

Hey Claes,

I really appreciate your response. I tried adding semi-colons to the end of my lines but unfortunately it didn’t help. I think the problem is passing the index to the function that has my array stored. I tried console.logging the index while the code runs through that function and it returns as “undefined.” So no wonder it’s not able to grab the nth value.
Do you think i’m using the async and await commands wrong? It seems like the might be trying to run that function before it gets the index even though the code is listed after it.

Am I able to grab a value from an array with a variable on a published site?

function get_url(i){
//undefined
console.log(i);
let url_array = [
    'https://www.ups.com/actrack/track/submit?awbNum=406',
    'https://freight.qantas.com/online-tracking.html?airWaybills=081-',         'https://www.cathaypacificcargo.com/ManageYourShipment/TrackYourShipment/tabid    /108/SingleAWBNo/160-',
    'https://www.airnewzealand.co.uk/international-cargo-track-and-trace',
    'https://www.brcargo.com/ec_web/Default.aspx?Parm2=191&Parm3=?TNT_FLAG=Y&AWB_CODE=695&MAWB_NUMBER='
];

//undefined
console.log(i);

//works
console.log(url_array);

return url_array[i];
}

Hi Claes,

I found a work around. Just added the URL’s the the airlines data set and queried that to pass along. I’m still concerned about the index thing though as what if there’s a situation with no such work around?
Either way, thank you for the suggestions.

i have this file the set fieldvalue woks in preview but not in live and this is a dashboard page. Also m using " Publishdataset" to populate the gridview
also i cannot get the dataset to refresh
any help will be greatly appreciated

by using the async and await function i got the Tabledatset to refresh but the repeater dataset still doesnt refresh

You need to use the field key in the setFieldValue() function, and not the field name. The field key starts with a lower case character.

i did however its working only on preview Mode

an update
after refreshing and syncing it works thanks a lot