Exploring the syntax of Vue's data function

I encountered a syntax error while compiling my assets:

Syntax Error: SyntaxError: C:\xampp\htdocs\dtcburger.com\resources\js\components\stripe\STRIPEform3.vue: Unexpected token, expected ";" (51:12)

  49 |     {
  50 |     stripe:null,
> 51 |     stripePK: stripePK,
     |             ^
  52 |     stripeStyles: stripeStyles,
  53 |     cardNumberElement:null,
  54 |     cardExpiryElement:null,

Here is how my code looks, could it be due to using ES syntax for component data?

Answer №1

To retrieve the object, you must encapsulate your function within parentheses and directly return the object literal:

data: () => (
  {
    stripe: null,
    stripePK: stripePK,
    //Include all other relevant properties here
  }
)

Another option is to employ the return statement:

data: () => {
  return {
    stripe: null,
    stripePK: stripePK
    //Include all other relevant properties here
  }
}

Answer №2

The primary cause of the issue is due to {} being recognized as a code block rather than an object literal. If you examine closely, the first stripe:null, property does not have a red squiggly underline. This is because it is interpreted as a labeled statement.

This scenario does not result in an error:

{
  stripe: null
}

However, the following will trigger an error stating Unexpected token ::

{
  stripe: null,
  stripePK: "stripePK"
}

To resolve this issue in your code, you can either use return within the function like this:

data: () => {
  return {
    stripe: null,
    stripePK: stripePK,
    ...
  }
}

OR, you can implicitly return from the arrow function by enclosing the object literal in parentheses:

data: () => ({
  stripe: null,
  stripePK: stripePK,
  ...
})

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

JQuery is unable to detect the response from PHP's echo statement

Receiving an 'ok' message from a PHP script through a JQuery ajax call has become quite the challenge for me. I am able to successfully display the correct message in the console when I log it, but for some reason, the corresponding jQuery funct ...

Exploring Django with Selenium: How to Extract Page Content Using find_element

I have been attempting to extract the text 'Incorrect Credentials' using selenium. Here is what I have experimented with... message_text = self.driver.find_element(By.XPATH, '//*[@id="toast-container"]/div/div[1][@class="ng-binding toast-t ...

Implementing form validations using JavaScript for a form that is created dynamically

I have dynamically created a form using javascript and now I need to add mandatory validations on the form. The validations should trigger when the dynamically created button is clicked. However, I am facing an issue where I receive an error whenever I t ...

Unable to establish SocketIO callback from client to server resulting in a null object instead

I'm encountering an unusual issue with SocketIO. On the server-side, I am using the emit() method like this: $s.sockets.emit(scope, {some: datas}, function(feedback) { console.log('received callback'); } ) ...

How come running `npm install <folder>` results in installing different dependencies compared to `npm install lib`?

My current project, project1, relies on <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5221262b3e37367f313d3f223d3c373c262112667c6">[email protected]</a>. When attempting to integrate project2 into project1 as a ...

Vue.js: In a third-party library, the reference to "this" is coming back as "undefined"

My third-party script contains code in the following format ( function initialMethod(scope, factory) { // 'scope' is undefined when used in Vue // but it works fine in React and Angular } (this, function function1(param1) { ...

Creating synchronicity in your code within the useEffect hook

Is there a way to ensure that my function is fully completed before moving on, even though it's not recommended to add async to useEffect? Take a look at this code snippet: useEffect( () => { const RetrieverDataProcess = async () => ...

Utilizing Material-UI components for a seamless navigation button experience

After reviewing the example on https://material-ui.com/guides/composition/#button, I have implemented a button in my main page: <BrowserRouter> <Box m={2}> <Button size="large" variant="cont ...

How can I represent the square bracket character using regex in JavaScript?

I'm trying to figure out how to create a regex for square brackets ( [ ] ) in JavaScript. Any advice? ...

What steps can be taken to prevent an event from being triggered once it has already started but has not yet concluded?

Let's talk about the navigation bar on the website. Clicking on it toggles the menu holder to slide in and out from the side. It's a simple interaction. The bar contains an icon with the text 'MENU'. When you open the menu, the icon cha ...

The Chrome browser allows interaction between two separate divs, where a button located in one div can impact

One issue I encountered involves a button located within a div that has unintended consequences for another div. Here is the basic structure of the elements: <div> <div> <div> <div> <butto ...

HTML Date input field not respecting locale settings and defaults to mm/dd/yyyy format

When using an html date input in an ng-repeat scenario, I noticed that it defaults to mm/dd/yyyy even though my computer's regional settings are set to dd/mm/yyyy. <script src="//unpkg.com/angular/angular.js"></script> <body ng-app&g ...

Elements on the page are quivering as a result of the CSS animation

When a user clicks anywhere on a div, I have added a ripple effect to give it some interaction. However, there is an issue where the element shakes and becomes blurry when the page is in full screen mode until the ripple effect disappears. Below is the Ja ...

Sophisticated method for implementing dynamic components with props in Vue

In a specific scenario, I am using an API to output all the content for my Page, including its structure. To provide context, imagine a CMS with a page builder feature where authors can place components via drag and drop to create pages, which are then del ...

Issue with displaying paragraph in Vue Js is resolved, however the checkbox functionality is still not working as expected

Upon mounting, I need to make the same POST API call three times and store the response in vuex. When I display the responses using: {{answer1}} // displayed 2 {{answer2}} // displayed 1 {{answer3}} // displayed 0 However, the checkbox does not wor ...

Utilize Restangular to send data as a payload rather than a query string

Currently, my backend service is set up to register a new user in our system using the following URL: /api/users/register When using Restangular, post requests are typically sent as either query string: Restangular.all('users').post("register" ...

Determine if two arrays of objects in JavaScript are equal and if so, store any new items in MongoDB

Hey there! I'm in the process of comparing two arrays of objects, each object containing an ID. One array is sourced from Stripe, while the other is retrieved from my database. The goal is to determine if the IDs match - returning true when they do an ...

Unable to access and process POST parameters sent via ajax in PHP

When passing two parameters in the send method to index.php, an error "Undefined index" is returned by PHP. echo $_POST['fname']; submit.addEventListener("click", function(e){ e.preventDefault(); var xhr = new XMLHttpRequest(); xhr. ...

Check if an array includes a specific value, and then either update it if found, or create it

I'm currently working with a Cart object in Javascript and I need to check if a specific item is present in the cart. Here's my approach: If the item is already in the cart, update its quantity. If it's not in the cart, add it to the items ...

Tips for maintaining the reference of a Three.js object after importing it as an .obj file

If you want to learn how to incorporate a .obj file into your scene, the official documentation provides a helpful example that can be found here. const loader = new OBJLoader(); // Load the resource loader.load( // Resource URL 'models/monst ...