Upload button working in editor but not in live.

I have an upload button which, after uploading the file, creates a property in my object adding its url.
All working well in the editor, but when I go live (chrome dev console), I get the error:
Cannot create property ‘justificantePago’ on string ‘[object Object]’.
I’m logging the file url and it’s the following:
wix:document://v1/34f7fa_e5a69265700743489ad17741f17bae6a.pdf/B1-Juliol 2019 .pdf

My code is:

$w('#enviar').onClick(() => {
        $w('#pujaJust').startUpload().then((file) => {
 //infoTrans is an object
 let infoTrans = session.getItem('infoTransAlumne');

 //Adding the property. Getting error here.
                console.log(file.url)
                infoTrans['justificantePago'] = file.url;

 //Guardem dades transacció
                wixData.insert('pagosMatricula', infoTrans).then(()=>{console.log('insertado')}, error=>{console.error(error.message)});
            })
            .catch(err => {
                console.warn(err.message);
                $w('#errorText').text = 'Error al pujar el justificant. Per favor, torna a intentar-ho.'
                $w('#errorText').show('fade', { 'delay': 2000 });
            })
    })

Which is a regular url of a wix document.
Any ideas?

Thank you in advanced!!

Hi! This link might help you. I used this link to help me figure out your problem on a clients website.
https://stackoverflow.com/questions/41510186/cannot-create-property-on-string

~Haiden~

Hi Dani :raised_hand_with_fingers_splayed:

I’m sure you’re awaiting my arrival :joy:

First of all, looks like you’re getting an object from the local storage, right?
But, Wix Storage only accept strings as a value, so the string value of the retrieved object will be [Object Object].

let infoTrans = session.getItem('infoTransAlumne');

So, “infoTrans” value is [Object Object], and you’re trying to access a value inside it, but we all know that the value is string, not an object and it has no items inside it whatsoever.

infoTrans['justificantePago'] = file.url;

And obviously you cannot read a value of a string, that’s why you’re getting the error.


Solution

I could have give you solution directly, but I wanted you to understand what’s causing the error, too.

First of all, and as I mentioned, Wix Storage APIs only accept strings as their value, so we need to JSON.stringify() the value before we save it, example:

// The object that you want to save.
let myObject = {
    id: 'some id',
    title: 'Your title',
    time: new Date().getTime();
}

// Stringify the object
let stringifiedObject = JSON.stringify(myObject);

// Saving the stringified object tosession memory.
session.setItem('infoTransAlumne', stringifiedObject)

Now that we have saved the object in the right way by processing it, we need to process it when we get it, too, example:

// Getting the object from session storage
let stringifiedObject = session.getItem('infoTransAlumne');

// Parse the stringified object to change it from text to an object
let infoTrans = JSON.parse(stringifiedObject);

And now you can use it as usual, and this line won’t return the error anymore.

infoTrans['justificantePago'] = file.url;

Oh! And by the way, you can access this property with the dot method:

infoTrans.justificantePago = file.url;

Just an additional note :wink:


You can always tag me in your posts, I’ll be glad to help if I could.

Hope that was helpful~!
Happy Coding </> :computer:
Ahmad

Awesome Ahmad, as always :slight_smile: Thanks again!!

You’re welcome Dani :blush:

The answer just got updated, pay attention to the changes.