How to resolve await issue when trying to call a function?

I am following an Ionic tutorial, & am trying to call this method:

upload(name: string) {

    return new Promise((resolve, reject) => {

      let ref = firebase.storage().ref('postImages/' + name);

      let uploadTask = ref.putString(this.image.split(',')[1], 'base64');

      uploadTask.on('state_changed', (taskSnapshot) => {

        console.log(taskSnapshot)

      }, (error) => {

        console.log(error)

      }, () => {

        uploadTask.snapshot.ref.getDownloadURL().then((url) => {

          firebase.firestore().collection('posts').doc(name).update({

            image: url

          }).then(() => {

            resolve();

          }).catch((err) => {

            reject();

          });

        }).catch((err) => {

          reject();

        });

      });

    });

  }

Here is how it is being called in the tutorial:

if (this.image) {
        await this.upload(doc.id);
      }

It is working fine in the tutorial, however I am getting this error message when I call await this.upload(doc.id)

Can someone please tell me how I can resolve this?

what error message are you getting?

make sure that if you use await inside a function, that function has async in front of it.

I’m guessing you’re doing something like

async uploadImage() {
   if (this.image) {
        await this.upload(doc.id);
      }
}

where uploadImage() is called from your template html?
make sure you’ve declared the function as async

I’ve said this a thousand times, and haven’t yet seen anybody who seems to want to listen, so maybe I’ll just give up, but one last time:

If you don’t completely understand what async and await do, and if you feel the need to ask a question on a community forum involving them, I would consider that an indication that you don’t, pretend they don’t exist. Don’t use them. Seriously. At all. You are not missing out on anything. They do not provide a magic time machine that turns asynchronous code synchronous. They provide zero unique or necessary functionality. And, most importantly, if you cargo cult them into your code without true understanding of what is going on behind the scenes, you are actively inhibiting your journey towards that understanding.

You also virtually never need to be manually instantiating Promises in an Ionic app. I’ve never used Firebase, but I’m sure there must be a quasi-standard library somewhere that delivers futures without application code having to deal with that.

need to see error message… and see code where you are calling function