Fetching and Uploading Information from Submodule - Vue.js/Axios/Laravel

I've been struggling to update data from a child component in a Vue.js Laravel application, and for some reason, I can't seem to get it to work correctly. The error message I'm getting in the inspector says:

Creating default object from empty value

The issue arises when the parent component opens a modal (which is a child component) and then attempts to update the information using the update() method. Can anyone help me figure out what I'm overlooking?

Here's an image of how my database is structured:

https://i.sstatic.net/L3KzT.png

The methods are located in my parent component Log.vue, and this is how I pass the data from the parent to the child component:

<log-edit v-if="editModalOpen" :logId="logId" :logUser="logUser" :logTitle="logTitle" :logType="logType" :logDescription="logDescription" :logDate="logDate" @closeRequest='close'></log-edit>

<td @click="openEdit(log.id, log.user, log.title, log.type, log.description, log.created_at)"><i class="fas fa-edit"></i></td>
<script>


methods:{
 openEdit(id, user, title, type, description, date){
                    this.logId = id;
                    this.logUser = user;
                    this.logTitle = title;
                    this.logType = type;
                    this.logDescription = description;
                    this.logDate = date;
                    this.editModalOpen = true;
                },
}

<script>

This is the EditLog.vue file, which is the child component receiving data from the parent above:

<template>
    <div class="container">
        <div id="overlay">
            <div class="edit-detail-window">
                <div class="modal-header">
                        <h3 class="modal-title" id="exampleModalLongTitle">{{logTitle}}</h3>
                        <button type="button" class="close">
                        <i class="fas fa-times" @click="close"></i>
                        </button>
                </div>
                <div id="show-detail-modal-body" class="modal-body">
                    <br>
                    <label>Title:</label>
                    <input class="form-control" type="text" v-model="logTitle">

                    <br>
                    <label>Type:</label>
                    <select v-model="logType" class="form-control" ><br>
                                <option value="" disabled selected>Type</option>
                                <option>Client Update</option>
                                <option>Dev Update</option>
                                <option>Bug</option>
                                <option>Style Fix</option>
                    </select>

                    <br>
                    <label>Description:</label>
                    <textarea class="form-control" cols="30" rows="5" v-model="logDescription"></textarea>

                </div>
                <div class="modal-footer">
                    <button d="log-it" type="button" class="btn btn-circle btn-xl" @click="update(logTitle, logType, logDescription)">
                        <span><b>EDIT</b></span>
                    </button>
                </div>
            </div>
        </div>
    </div>
</template>


<script>

import axios from 'axios';

export default {

    name:'EditLog',

    props:['logId','logUser','logTitle','logType','logDescription','logDate'],

    data(){
        return{
            log:{title:'',type:'',description:''},
            errors:{}
        }
    },

    methods:{

        close(){
            this.$emit('closeRequest');
        },

        update(title,type,description){

            this.log.title = title;
            this.log.type = type;
            this.log.description - description;

            window.axios.patch(`/develogger-app/public/api/logs/${this.logId}`,this.$data.log).then((response)=> this.close())
                    .catch((error) => this.errors = error.response.data.errors)

        }

    }
}
</script>

This is the code snippet from Log routes/api.php:

Route::patch('/logs/{id}','LogController@update');

And here is the update function in LogController.php:

public function update($id, Request $request){

        $log = Log::find($request->id);
        $log->title = $request->logTitle;
        $log->type = $request->logType;
        $log->description = $request->logDescription;

        $log->save();

    }

Any suggestions on how to resolve this issue would be greatly appreciated.

Answer №1

Upon reviewing the content, a few key points stood out that may have been too extensive to include in a standard comment.

Primarily, rather than individually passing all properties of a log to the <edit-log> component, it could be more efficient to pass the entire object instead?

<edit-log :log="log"></edit-log>

Additionally, it seems that you are not properly binding the prop data being sent to <edit-log> with the data attribute on that specific component. Directly applying v-model to a prop might not function as expected.

Furthermore, when performing an update within the <edit-log> component, ensure to simply pass the data using something like this.log rather than this.$data.log.

This is how your complete <edit-log> section might appear:

<template>
    <div class="container">
        <div id="overlay">
            <div class="edit-detail-window">
                <div class="modal-header">
                        <h3 class="modal-title" id="exampleModalLongTitle">{{logTitle}}</h3>
                        <button type="button" class="close">
                        <i class="fas fa-times" @click="close"></i>
                        </button>
                </div>
                <div id="show-detail-modal-body" class="modal-body">
                    <br>
                    <label>Title:</label>
                    <input class="form-control" type="text" v-model="log.title">

                    <br>
                    <label>Type:</label>
                    <select v-model="log.type" class="form-control" ><br>
                                <option value="" disabled selected>Type</option>
                                <option>Client Update</option>
                                <option>Dev Update</option>
                                <option>Bug</option>
                                <option>Style Fix</option>
                    </select>

                    <br>
                    <label>Description:</label>
                    <textarea class="form-control" cols="30" rows="5" v-model="log.description"></textarea>

                </div>
                <div class="modal-footer">
                    <button d="log-it" type="button" class="btn btn-circle btn-xl" @click="update()">
                        <span><b>EDIT</b></span>
                    </button>
                </div>
            </div>
        </div>
    </div>
</template>


<script>

import axios from 'axios';

export default {

    name:'EditLog',

    props:['initiaLog'],

    data(){
        return{
            log:this.initialLog,
            errors:{}
        }
    },

    methods:{

        close(){
            this.$emit('closeRequest');
        },

        update(){

            window.axios.patch(`/develogger-app/public/api/logs/${this.logId}`,this.log)
                .then((response)=> this.close())
                .catch((error) => this.errors = error.response.data.errors)

        }

    }
}
</script>

To initialize it, you would use this syntax:

<log-edit v-if="editModalOpen" :initial-log="log" @closeRequest='close'></log-edit>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Node.js readline: SyntaxError: Unexpected token =>

Currently, I am diving into node.js and have found myself in need of utilizing the readline module for a new project. Below is the code snippet that I extracted directly from the official readline module example. const readline = require('readline&ap ...

Calculating the time difference between two dates in the format yyyy-MM-ddTHH:mm:ss.fffffff can be done by following these steps

Can someone help me figure out how to calculate the difference in days between the date and time 2021-02-23T08:31:37.1410141 (in the format yyyy-MM-ddTHH:mm:ss.fffffff) obtained from a server as a string, and the current date-time in an Angular application ...

JSP checkbox functionality

I have been attempting to solve this issue since last night, but I am struggling and need some help. There are two pages, named name.jsp and roll.jsp. In name.jsp, there are two input text boxes and one checkbox. After entering data in the text boxes and ...

Guide to showcasing an image on an HTML page using HTTP header

I have an HTML page with a basic layout that includes a single button. When this button is clicked, it needs to trigger an HTTP request to a specific URL while including an Accept header for GET requests. The response from the HTTP URL will vary depending ...

Calculate the Pythagorean Theorem with HTML and JavaScript

I'm currently attempting to create a Pythagorean theorem calculator using HTML and Javascript. The goal is for the calculator to determine any side when given two side values. I've implemented if statements in my code, but for some reason, it doe ...

Guide on how to halt camera animation in three.js when it reaches a specific position upon clicking

I'm currently working on a small piece of code that animates the camera to zoom in on an earth model when clicked. However, I'm facing an issue where I need to stop the camera animation once it reaches a certain position. Initially, the camera p ...

Converting MySQL data into JSON format by extracting column names

Having some difficulty parsing the MySQL query output to use with Highcharts. The challenge lies in needing to handle over 100 data series without explicitly referencing each column name during parsing. Ideally, the desired output should look like this: [ ...

Issues with displaying HTML video content

Seeking assistance with a unique coding predicament. I've got an HTML file that showcases a JavaScript-powered clock, but now I'm looking to enhance it by incorporating a looped video beneath the time display. Oddly enough, when the clock feature ...

Is ES7 going to emulate full synchronous behavior in this function call?

In the given scenario: I have a wrapper function called "InitializePage()" that gets called when the page is loaded. Within this function, there are functions a-d. Functions a and b both involve AJAX calls while c and d are synchronous operations. The stru ...

Using addClass and removeClass functions in JQuery when selecting a radio button

I am facing an issue with my radio buttons being displayed as images. When a picture (radio button) is selected, I want the other pictures to become hidden. To achieve this, I plan to add a class to the checked picture and then add another class to the unc ...

Guide to efficiently inserting multiple records in Microsoft SQL Server

I'm currently working on executing a bulk insert in MYSQL. Below is the snippet of my code: const values = workouts.map((workout) => [ nanoid(), programEnrollmentId, workout.workout_id, userId, ]); a ...

Combine 2 MySQL databases while ensuring no duplication of data occurs

My database consists of 4 tables, and I have been inputting data into it using PHP on my local machine. To collaborate with a coder who is in a different location, I uploaded my PHP scripts and the database to a shared hosting site. I realize that we can ...

Is it possible to achieve a seamless change using show/hide jQuery functions when hovering over an

How can I achieve smooth transitions when using an image map to show a div on hover, similar to the functionality seen on this website: ? Below is my current JavaScript code: var mapObject = { hover : function(area, event) { var id = area.attr ...

MUI Autocomplete causing a never-ending cycle of requests

One of the challenges I'm facing involves an Autocomplete component in my code. Here's a snippet of the code: <Autocomplete filterOptions={(x) => x} options={items} getOptionLabel= ...

What is the best way to keep vnodes saved in the browser indefinitely?

Is there a way to redirect pages in Vue.js without refreshing the page while also keeping the component alive? I've tried using JSON.stringify(vnode) to store the vnode but I keep getting a TypeError: Converting circular structure to JSON error. TypeE ...

How can we universally detect and resolve the user's language within a React/Next.js application using an Apollo client HOC?

Currently, I am developing an I18n module utilizing the Next.js framework (v5). The challenge I am facing is determining the user's default language universally in order to display the UI in that language. While it is relatively simple to resolve th ...

Forecasting and rectifying complete text retrieval

I have successfully implemented a full text search feature in a web application, but now I want to enhance it by adding a prediction/correction feature. This feature would suggest a corrected version of the user input if it yields zero results or contains ...

Create dynamic object assignments within JavaScript objects

Check out this object sample, I want to iterate through and dynamically assign {"type": "object"} within each inner object. Input: var favoriteFruit = { "Amy": { "desc": "Amy's fav", "fruit": { "Name" : "Banana", ...

Javascript: Executing a interval, delay, and another interval

My challenge is to reveal a list of elements one after another using setInterval for 5 seconds. However, I want to introduce a 60-second timeout after the 7th element and then continue the interval without repeating the timeout for every 7th element. Here ...

Enhance the flight experience of a helicopter with jQuery game by adding smoother controls

I'm currently experimenting with creating a basic game. In this game, the user controls a helicopter and can fly it up and down. Although I have successfully implemented the basic functionality where holding the screen makes the helicopter ascend an ...