Leveraging a props Variable within an asynchronous Function

Within a vue.js component, I have successfully received the value of web3. However, when attempting to use it in an async function, I am encountering an undefined error on the variable. Is there something missing or should it be possible to use it as shown below? (blockNum is already assigned a value earlier in the code.)

 props: ['web3']

 async function getTimestamp () {
     const block = await this.web3.eth.getBlock(blockNum)
     const ts = await this.web3.eth.getBlock(block).timestamp
     return ts
 }
 console.log(getTimestamp())  

Answer №1

When dealing with the getTimestamp async function, it's important to note that it returns a promise rather than an immediate value. To retrieve the timestamp, you must first wait for the promise to be resolved before outputting it using console logging.

async function getTimestamp () {
 const block = await this.web3.eth.getBlock(blockNum)
 const ts = await this.web3.eth.getBlock(block).timestamp
 return ts
}

getTimestamp().then(ts => {
    console.log(ts)
}

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

Move to the next input field once the maximum length is reached

Is there a way to automatically shift focus to the next input field once the previous input has reached its character limit? a: <input type="text" maxlength="5" /> b: <input type="text" maxlength="5" /> c: <input type="text" maxlength="5" / ...

Is there a way for me to scroll to #target but minus 100px from its position

Trying to implement: <a href="#target">Details</a> <div id="target">asd...</div> The issue is, I also have a top popup menu that overlaps. Is there a way to adjust the JavaScript (I am already using bootstrap and jQuery libraries ...

Ways to update property values of an array object in JavaScript

I am trying to match values from one array object with another array and update the status if there is a match. Here's the desired output for arrObj: [ { name: "Test1", status: true }, { name: "Test2", status: false }, { name: "Test3", s ...

Traverse through a series of selected options in an AJAX call to

I am facing a dilemma with a multiple select item, as it allows me to choose and send multiple selected items via an ajax request for importing into my database. The challenge lies in looping through the array received from the POST request. Ajax Request ...

Utilize Launchdarkly in React by incorporating it through a custom function instead of enclosing it within

Currently, I am adhering to the guidelines outlined by launchdarkly. Following the documentation, I utilized the following code snippet: import { asyncWithLDProvider } from 'launchdarkly-react-client-sdk'; (async () => { const LDProvider = a ...

Refusing to invoke a Python function from JavaScript with the likes of an e

I'm having an issue with a toggle feature in my Eel application. Unfortunately, when I try to print "Test" to the terminal, it doesn't seem to work as expected. <div class="container-afk"> <label class="toggle_box" ...

How can I call a function in the parent component when clicking on the child component in Angular 1.5?

In my Angular 1.5 app, I have two components. The first one is the parent: angular. module('myApp'). component('myContainer', { bindings: { saySomething: '&' }, controller: ['$scope', functio ...

Loop through an array of buttons using an event listener

My HTML and Javascript code currently have the functionality to show/hide a row of 3 images upon button click. However, I need this feature to work for two additional rows similar to this one. I believe it involves looping through an array of buttons, but ...

The Django REST API gladly accepts posts from Swagger, however, it is not compatible with postman or vue form when using axios for

I am facing an issue with my Django model where I can add records using the Admin interface or Swagger POST, but a Vue form is returning a mysterious code 400 error without any explanation. I attempted to debug using Postman, only to receive the message "d ...

The port has not been defined

My Node server appears to be operational, however the console is displaying an error message stating that the port is undefined. const express = require('express'); const env = require('dotenv') const app = express(); env.config(); ap ...

How can you globally configure Highcharts objects with vue-highcharts?

Is there a way to globally set chart options for Highcharts in a Vue application? I have installed the following plugins: import Vue from "vue"; import Highcharts from "highcharts"; import VueHighcharts from "vue-highcharts"; import addFunnelModule from " ...

Which option offers better performance in Three.js: utilizing a sprite or a BufferedGeometry?

I am currently in the process of revamping an outdated visualization created a few years back using an earlier version of Three.js. The visualization consists of approximately 20,000 2D circles (nodes from a graph) displayed on a screen with predetermined ...

Is it possible to import Node Packages in Vue without using the Composition API?

Struggling to grasp the concept of utilizing external Node Packages has been a frustrating experience for me lately. My current endeavor involves integrating TinyMCE. After installing all the necessary packages, I attempted to follow this example: <scr ...

Have you ever encountered the frustration of being unable to navigate to the next page even after successfully logging in due to issues

I'm currently utilizing Vue and Firebase to build my application. One of the features I want to implement is the redirect method using vue-router. Within my vue-router code, I've included meta: { requiresAuth: true } in multiple pages as middlew ...

The .NET controller does not receive traffic for the GET method

Having some trouble populating a table with JSON data using angular ng-repeat. No errors are showing up in the console, and my breakpoint in the .NET Controller isn't being triggered. Here's the code for the App Controller: var app = angular.mo ...

Transforming DOM elements into Objects

Here are some values that I have: <input name="Document[0][category]" value="12" type="text"> <input name="Document[0][filename]" value="abca.png" type="text" > I am looking for a way to convert them into an object using JavaScript or jQuer ...

Replace existing styled-component CSS properties with custom ones

One of my components includes a CheckBox and Label element. I want to adjust the spacing around the label when it's used inside the CheckBox. Can this be achieved with styled()? Debugging Info The issue seems to be that the className prop is not b ...

The angular variable is being updated, but the tweet embed code surrounding it is not being refreshed

I've been working with Angular to loop through an array of tweet ids and display them on the page using Twitter's embed code. The issue I'm facing is that although the variable gets updated, the twitter embed remains static. Here's a gl ...

Are foreign characters the culprit behind the JSON parsing problem?

I am encountering an issue while attempting to parse a field containing JSON data returned from an API. The data includes various unusual characters like East Asian symbols and curly quotes, causing this error to appear. I am unsure of how to resolve it - ...

What is the best way to add a modal to every loop in a Jade template?

I've been working on a blogging engine using Node.js and jade. I have a section where I iterate through each post and add a button that allows users to delete the post. However, I've encountered a strange issue - when I click on the delete button ...