The setup function raises an error claiming that the object is not defined, despite the fact that it is clearly defined within the function and successfully

Why am I getting an error that says newBlog is not defined? I have defined it in the setup function and used it in the event handler function. What could be causing this issue?

<template>
  <form @submit="onSubmit" class="add-form">
     ...
  </form>
</template>

<script>
import { ref } from '@vue/reactivity'
    export default {
        setup() {
            const title = ref('')
            const body = ref('')
            
            const onSubmit = (e) => {
                e.preventDefault()

                const newBlog = {
                    title: title,
                    body: body
                }

                this.$emit('add-blog', newBlog)

                title.value = ''
                title.body = ''
            }
            return { title, body, onSubmit, newBlog }

        }
    }
</script>

Answer №1

newBlog variable is defined within the onSubmit function scope, making it inaccessible outside of that function, which is causing the error you encountered.

To resolve this issue, you should move the declaration of newBlog outside of the onSubmit function like so:

export default {
  setup() {
    //...

    const newBlog = {...}

    const onSubmit = (e) => {
      this.$emit('add-blog', newBlog)
    }

    return { title, body, onSubmit, newBlog }
  }
}

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

Adjusting the height of a flexbox column to fit three rows within the space of two

Exploring the wonders of Flexbox and delving into its functionality. I have shared a Code Sandbox link showcasing my React /bootstrap code in progress... Currently, I am developing a clock component with two buttons for adjusting time (increase/decrease). ...

Leveraging JavaScript along with the jQuery library and API to showcase information related to

Hey there! I have been working on this API that shows upcoming concerts, but I'm struggling to display the images associated with each concert. I've tried using for loops to iterate through the objects, and it seems like every sixth element has ...

Get the Google review widget for your web application and easily write reviews using the Google Place API

I developed a platform where my clients can provide feedback and ratings on my services through various social media platforms. Currently, my main focus is on collecting Google reviews using a Google widget/flow. https://i.sstatic.net/RvPst.png The imag ...

Adjust the color of the entire modal

I'm working with a react native modal and encountering an issue where the backgroundColor I apply is only showing at the top of the modal. How can I ensure that the color fills the entire modal view? Any suggestions on how to fix this problem and mak ...

Linking to an external website using AngularJS for navigation

After developing my angular app, I included an array of menu items that are displayed in the template: <nav id="menu"> <ul> <li ng-repeat="i in menuItems" ui-sref="{{ i | removeSpacesThenLowercase }}" ui-sref-active=" ...

Guide to converting a form into a structured object format (with a tree layout)

Looking to transform a form into an object structure? <form> <input type="text" name="Name" /> <input type="checkbox" name="Feature.Translate" /> <input type="checkbox" name="Feature.Share" /> <input type="submi ...

Generate a Monaco Editor within a Vue.js component

Currently, I am integrating Monaco Editor with Vue.js and facing some confusion regarding how Monaco is being instantiated within the Vue component: 1) In my data() method, I have defined an editorEx object to be used for this purpose, like so: data() { ...

MQTT Broker specialized in Typescript

I'm looking to create a MQTT Broker using TypeScript and Angular. I've attempted a few examples, but I keep encountering the following error: Uncaught TypeError: http.createServer is not a function Here's a simple example of what I'm c ...

Guide to comparing the contents of two text fields and highlighting the altered characters using ReactJS

Is there a way to compare the contents of two Material-UI textfields and identify the characters that have changed in both? I came across a similar question, but it was specifically for ReactJS rather than C# Windows Forms: How can you highlight the chara ...

Tips for updating documents in MongoDB using NodeJS

I'm trying to update the document using the command below. order = await db.collection("orders").findOneAndUpdate({ order_id: req.body.ORDERID }, {$set: { payment_status: "Paid", paymentInfo: JSON.stringify(myrequest) }}) console. ...

preclude any dates prior to the chosen date

I need a solution for a scenario where I have 5 datepickers in sequence. When I select a date on the first picker, all previous dates should be disabled when selecting on the next picker. Additionally, once a date is selected on one picker, the following ...

Issues with data retrieval from PHP file in AJAX submission

During my attempts to utilize AJAX for submitting data to a PHP file, I encountered an issue where the submission was successful and I could receive a message echoed back from the PHP file. However, when trying to echo back the submitted data or confirm if ...

Include and run a series of scripts in the package.json file

Exploring Node.js for the first time, I find myself in need of adding a series of scripts to package.json and running them one by one. Is this achievable with Node.js? "scripts": { "sample": "run --spec '*spec.js'&quo ...

In what scenarios is it most beneficial to utilize an isolate scope in Angular?

The AngularJS guide states that the isolate scope of a directive isolates everything except models explicitly added to the scope: {} hash object. This is useful for building reusable components because it prevents unintended changes to your model state, al ...

React MUI multi select does not allow for deselecting all items at once

Currently, I am experimenting with the MUI "multiple select" / "multiple checkbox select" functionality. Objective: -> To open a modal, set an initial value with setState, and then have complete control over the select. Challenges: -> At the moment ...

Only a select few expandable elements in the jQuery accordion

How can I create an accordion menu with only a few expandable options? I am looking to include the following items in my menu: Home, Support, Sales, Other The "Home" option is just a link without any sub-menu. Clicking on it should direct users to a spec ...

Angular.js: automatically select default option based on ID

In my angular.js single page application, I am retrieving initial data from a rest endpoint. The data consists of a list of IDs representing saved models and a tree of options for cascading dropdowns. How can I automatically set the default path in the t ...

Send form using AJAX with a callback function

I need help figuring out how to submit a form when a captcha is clicked. I attempted to use my own jQuery function, but unfortunately it's not working. Could someone please take a look at my code and let me know what's wrong with it? Javascript ...

What is the process to modify the font color on a webpage using a button?

I need some help figuring out how to change the font color on my website when someone clicks a button. I already have the background functionality working, but I can't seem to get the text color to change. This is the code I have so far: <div cl ...

Using Console.log() will display 'undefined' prior to receiving any data

I am facing a problem with a lifecycle hook that I have been trying to troubleshoot. export default class EditRevision extends Component { state = { data: [], customColumns: [] } componentWillMount = () => { axios.get('http:/ ...