2

I am new in JS (and vue too) and have problem witch fetch data. My target is - want to save all data from API and using them in functions.

Code:

<div>{{ dotToComma }} kg</div>

Vue:

export default {
  data() {
    return {
      weight: null
    };
  },

  created() {
    this.api();
  },

  methods: {

    //this is working correctly
    async api() {
      const api = await fetch("/reporting/patient/values");
      const data = await api.json();
      this.weight = Math.round(data.weight * 10) / 10
    }
  },

  computed: {
    //this.weight is still null
    dotToComma() {
      return this.weight.replace(".", ",");
    }
  }
};

Where is the problem? Thank you

UPDATE:

function replace in dotToComma is working only for strings, so you have to add toString() function:

return this.weight.toString().replace(".", ",");
1
  • Not worked on Vue.. may be try to call dotToComma with () like <div>{{ dotToComma() }} kg</div> Commented Mar 3, 2021 at 10:42

1 Answer 1

2

Async functions won't give you result instantly, so you need to check do you have this.weight, or not. Computed will use new value, when it come. You can read more about JS async realization here

Sign up to request clarification or add additional context in comments.

Comments

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.