Tukei David
Tukei David

Reputation: 101

[Vue warn]: Unhandled error during execution of mounted hook

I am creating a to-do web app. I have successfully fetched all the to-do list items and displayed them on my homepage. The problem comes when I try to fetch only one post and display it, which returns hook is not a function error.

I am using laravel on the server side and vite on the client side.

Below are my api.php routes

<?php

use App\Http\Controllers\TodoController;
use App\Http\Controllers\TodosController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
    return $request->user();
});


Route::get('/todos', TodosController::class);
Route::get('/todos/{todo:uuid}', TodoController::class);

On the third route, I used uuid to get the individual post and display it. which displays correctly on server side

Below are my TodoController.php and TodoResource respectively.

<?php

namespace App\Http\Controllers;

use App\Http\Resources\TodoResource;
use App\Models\Todo;
use Illuminate\Http\Request;

class TodoController extends Controller
{
    //
    public function __invoke(Todo $todo)
    {
        return new TodoResource($todo);
    }
}

TodoResource.php

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class TodoResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        return [
            'uuid' => $this->uuid,
            'name' => $this->name,
            'completed' => $this->completed,
            'completed_at' => $this->completed_at,
        ];
    }
}

I have fetched the post in client server side using axios. Below is the codes for useTodos.js

import { ref } from 'vue'

import axios from 'axios'


export default function useTodos() {
    const todos = ref([])
    const todo = ref([])


    const fetchTodos = async () => {
        let response = await axios.get('/api/todos')
        todos.value = response.data.data
    }


    const fetchTodo = async (uuid) => {
        let response = await axios.get(`/api/todos/${uuid}`)
        console.log(response)
        todo.value = response.data.data
    }


    return {
        todos,
        fetchTodos,
        todo,
        fetchTodo
    }
}

Below is the page for showing a single post in client-server side

<template>
    <div>
        {{ todo }}
    </div>
</template>

<script>

import useTodos from '../api/useTodos'
import { onMounted } from 'vue'

export default {
    props: {
        uuid: {
            required: true,
            type: String
        }
    },
    setup(props) {
        
        const { todo, fetchTodo } = useTodos()

        onMounted(fetchTodo(props.uuid))

        return {
            todo
        }
    },
}
</script>

My router page

import { createRouter, createWebHistory } from 'vue-router'

import Home from '../pages/Home.vue'
import Todo from '../pages/Todo.vue'

const routes = [
    {
        path: '/',
        name: 'home',
        component: Home
    },
    {
        path: '/todos/:uuid',
        name: 'todo',
        component: Todo,
        props: true
    }
]

export default createRouter({
    history: createWebHistory(),
    routes
})

I have tried to find the solution to why it is bringing the hook is not a function when i try to fetch single post. Can someone help me on this problem?

Upvotes: 0

Views: 3261

Answers (1)

kissu
kissu

Reputation: 46696

Try to run it that way

async onMounted(() => {
  await fetchTodo(props.uuid)
})

As shown here: https://vuejs.org/api/composition-api-lifecycle.html#onmounted

Upvotes: 1

Related Questions