0

I'm trying to display a result from an async method in the Vue/Ionic Template. I successfully retrieve the value from the local storage and output it using console.log

 setup() {

 import {Plugins} from "@capacitor/core";
 const {Storage}=Plugins;

 const getEmail= async()=>{
          const emailv=await Storage.get({key: "email"});
          console.log(emailv.value);
          return emailv.value;
    };


 }

I also tried this

 setup() {

  import {Plugins} from "@capacitor/core";
  const {Storage}=Plugins;

   const emailv= async()=>{
          const {value}=await Storage.get({key: "email"});
          console.log(value);
          return value;
    };
}

And that's how I call it on my template:

<ion-note>{{emailv()}}</ion-note>

The problem is that, although it's being successfully displayed on the console, it doesn't seem to work on the template. It just shows me "[object Promise]". I have tried looking for some solutions and, from what I understood (please correct me if I'm wrong as I'm not sure how async-await works.), once I retrieve the variable, I should be able to use it everywhere.

So my question is:

Is there any way for me to "get" this variable and display it in the template?

4
  • please share the whole code that includes this function Commented Feb 5, 2021 at 11:31
  • Thank you. I will update my question but there's nothing much to show actually. I'm using imports. It works, It just doesn't display the value correctly. Commented Feb 5, 2021 at 11:32
  • are you using this in setup hook? Commented Feb 5, 2021 at 11:33
  • Yes. I'm using it in setup hook. Commented Feb 5, 2021 at 11:34

2 Answers 2

1

Can you try this ? Put your setup async and await the result in your const :

async setup() {
  const emailv = await getResults()

  function getResults() {
    return Storage.get({key: "email"})
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You should call that function inside the mounted hook then just use emailv in your template :

import {ref,onMounted} from 'vue';
 import {Plugins} from "@capacitor/core";
 const {Storage}=Plugin

export default{

setup(props){
  const emailv=ref('')

  onMounted( async ()=>{
     const {value}=await Storage.get({key: "email"});

     emailv.value=value;
   })

  return{
   emailv
  }
}
}

template :

<ion-note>{{emailv}}</ion-note>

9 Comments

120:5 error 'onMounted' is not defined no-undef
did you add import {ref,onMounted} from 'vue';?
I just did, thank you. It console.logs the value but doesn't display anything on the template. Now it shows nothing.
could you share the output of console.log(emailv.value); after emailv.value=value;
what about onMounted( ()=>{ Storage.get({key: "email"}).then(res=> emailv.value=res.value;}) })?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.