[Vuejs]-Cannot set properties of undefined (setting 'projects')

0πŸ‘

βœ…

You don’t want to create a new function in methods, just add your code (but make sure mounted is async):

<script >
export default {
  data() {
    return {
      projects: [
        
      ]
    }
  },
  async mounted() {
    const data = await fetch("https://jsonplaceholder.typicode.com/posts");
    const newData = await data.json();
    console.log(newData)
    this.projects = newData;
  }
}
</script>

You could do this:

  async mounted() {
    this.projects = await fetch("https://jsonplaceholder.typicode.com/posts").then(raw => raw.json());
  }

Example

πŸ‘€tauzN

3πŸ‘

The error is that you are using this within a function () notation. In order to make it work as you expect, you should use an arrow function.

const test = async () => {
//...
}
πŸ‘€Lykos94

0πŸ‘

First, test() should be a method. Second, place the closing bracket in projects on the same line.

πŸ‘€Eze Kohon

Leave a comment