Displaying data from asynchronous functions in Vue.js

As a beginner in VueJs, my goal is to develop a basic application that fetches text from a Wikipedia page and displays it upon clicking a button.

Code

<script>
const wiki = require('wikipedia');
export default {
    data() {
        sum:"hello"
    },
    methods: {
        async getWiki() {
            try {
                const page = await wiki.page('Batman');
                console.log(page);
                //Response of type @Page object
                const summary = await page.summary();
                console.log(summary.extract);
                //Response of type @wikiSummary - contains the intro and the main image
                this.sum = summary.extract
            } catch (error) {
                console.log(error);
                //=> Typeof wikiError
            }
        }
    }
}
<template>
    <span>{{sum}}</span>
    <button @click="getWiki()">Get Wiki</button>
</template>

Errors

[Vue warn]: Property "sum" was accessed during render but is not defined on instance. at <Main>

and

[Vue warn]: data() should return an object. at <Main>

However, all console.log statements function correctly when the button is clicked

Answer №1

Make sure to include a return statement when defining your data

data() {
  return {
    sum: "hello"
  }
}

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

How can I generate a dummy JSON response using Backbone Fetch?

Exploring Backbone and looking for a way to simulate the response of a .fetch() call within a model without using a testing library or connecting to an external service. If the setting in the model is this.options.mock === true, I'd like to use an in ...

The Vue.js modal is unable to resize below the width of its containing element

My challenge is to implement the Vue.js modal example in a larger size. I adjusted the "modal-container" class to be 500px wide, with 30px padding and a max-width of 80%. However, I'm facing an issue where the "modal-mask" class, containing the contai ...

Retrieve all HTML elements within a Vue slot using JavaScript

In VueJS 2, I am in the process of developing a unique select component. The component will be utilized by the end-user in the following manner. <custom-select> <option value="value 1">Option 1</option> <option value=&quo ...

Searching for an item in an array within MongoDB: Tips and techniques

I am attempting to locate the object containing a specific element within an array: Here is my query: router.get('/', function(req, res) { var userSubscribed = req.user.email; db.posts.find({ "SubscriberList": { $elemMatch: userSubscrib ...

Exploring the Possibilities of Utilizing Async Middleware for Error Handling in VueJS Router

Greetings, this is my inaugural question on Stack Overflow. For my Frontend, I am utilizing VueJS and for Backend, Laravel. My current predicament involves loading a model from the backend API that requires user editing. However, there are policies in pla ...

What role does a computed property play in enabling reactivity within a Vue component utilizing a Pinia store?

I have a web application built with Vue and Supabase, where I am storing an authenticated user in a Pinia store called userStore.user. The NavBar component in my app has a login/logout link that is based on the state of the user. To make this reactive with ...

Retrieve the tweets from the next Twitter user in the Array using blogger.js

I am working on a project that involves a javascript array of Twitter usernames. Using blogger.js, I successfully load the first username along with their last 5 tweets. Now, I am looking to create HTML buttons or links for "previous" and "next" in order ...

Add numerous submit buttons within a form. Upon clicking on a button, the form should be submitted to a specific URL using AJAX

I have a form with two submit buttons: one labeled as "SUBMIT" and the other as "SCHEDULE NEXT ROUND". When a user clicks on the "SUBMIT" button, the form values should be stored in the database and redirect to the view page. If they click on the "SCHEDULE ...

Unable to observe the transition of opacity changes in threeJS for tweens

<script type="importmap"> { "imports": { "three": "https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f2869a809797b284c2dcc3c4c0dcc2">[email protected]& ...

Place the item at the top of the list before scrolling downwards

I'm struggling with what seems like it should be a simple task. My goal is to add new items to the top of a list and have the list scroll down to show the new item. So far, I've managed to create and add a new list item using this code snippet: ...

Enhance the form values using HTML and Javascript before submitting them via POST

Greetings to all! Apologies if my question seems elementary, as I am relatively new to using Javascript/HTML... I am encountering an issue with PayPal integration. In the past, I have successfully implemented it with a single fixed price, but now I have ...

Tips for utilizing ui-sref in a complex Angular UI router configuration

After spending hours researching and implementing different techniques, I am still unable to solve the issue. My application consists of three main navigations: home, sports, and country. <head> <script src="js/angular-ui-router.min.js"></s ...

Encountering an issue when bundling an application using electron-forge: "Unable to find solution for './→' "

Having trouble packaging my Electron application using electron-forge. It seems like the issue lies with native modules such as 'sqlite3'. Upon running the command npm run package, I encounter this error: Module not found: Error: Can't reso ...

Having trouble updating the route with the $location service, even after attempting to use $scope.apply

After trying to utilize the location service with no success, I am left wondering why my view isn't changing even after using either $scope.$apply() or $scope.apply. Prior to posting my question, I conducted thorough research on similar inquiries but ...

What are the steps to creating a unique event within an AngularJs service?

In the midst of my AngularJs project, I find myself faced with a dilemma. I have a service designed to manage button events, but I want another service to handle these events indirectly without directly interacting with the buttons themselves. So, my goal ...

Commitment failing to deliver the information

I am currently attempting to retrieve data based on a specific ObjectId using promises. The function is successfully fetching the data from the database, but it is failing to return the result as expected. Below is the code snippet: getScreenDetails = fun ...

Unpredictable Behavior of CSS Transition with JS createElement() Function

I'm using JavaScript to create a div element and I'm adding the CSS property transition: .5s linear top; When the user clicks on the element (onmousedown event), it is supposed to smoothly move to the top of the screen and then be deleted using ...

updating a div with URL redirection instead of global redirect

I am facing an issue with redirecting my website flow to the login page when a user clicks any link on the page after the session has expired (either due to timeout or manual logout from another window). In an attempt to solve this, I inserted the followi ...

Understanding the distinction between assigning a value and setting text through JSE in Selenium using C#

Utilizing the IJavaScriptExecutor to set the attribute value can sometimes result in the text box containing the set value, but not displaying it as text. In some cases, the input is sent normally to certain text boxes, while for others, it is only setting ...

Opening a fresh window with HTML content extracted from the existing page

Is there a way to open a new window and transfer some of the HTML content from the original page to the new window? For example: $("div#foo").click( function(){ var copyHTML = $("table.bar").html(); window.open(''); // how can we ...