It seems like the method has already been executed despite encountering an unexpected end of JSON input error

Currently, I am utilizing the etherscan API to retrieve logs for certain events. Although my JSON parsing method seems quite traditional, an error is being thrown stating "unexpected end of JSON".

function getEventHistory() {
    const topic0 = web3.utils.keccak256(config.event_string);
    document.getElementById("topic0").innerHTML = topic0;
    const qry = ("https://api.etherscan.io/api?module=logs&action=getLogs"+
                    "&fromBlock="+10338000+
                    "&toBlock="+"latest"+
                    "&address="+config.address+
                    "&topic0="+topic0+
                    "&apikey="+config.ether_api);
    console.log(qry)
    const request = new XMLHttpRequest()
    request.open("GET", qry);
    request.send();
    request.onreadystatechange=(e)=>{
        // document.getElementById("rLogsResp").innerHTML = request.responseText;
        parseResponse(request.responseText);
    }
}

function parseResponse(jsonString) {
    console.log("Converting string to jsonString: "+jsonString);
    const jsonObj = JSON.parse(jsonString)
    console.log("Full Response: "+jsonObj);

    var logs = []
    for (var i = 0; i < jsonObj.result.length; i++) {
        console.log("Element Number: "+i);
        var event = jsonObj.result[i];
        console.log("Index: "+event);
        const parsedEvent = new Event(event);
        logs.push(parsedEvent)
        console.log(parsedEvent);
    }
}

class Event {
    constructor(event) {;
        console.log("Parse address: "+event.address);
        this.address = web3.utils.toAscii(event.address);
        console.log("Parse topics: "+event.topics);
        this.topics = [web3.utils.toAscii(event.topics[0]), web3.utils.toAscii(event.topics[1]), web3.utils.toAscii(event.topics[2])];
        console.log("Parse data: "+event.data);
        this.data = web3.utils.toAscii(event.data);
        console.log("Parse blockNumber: "+event.blockNumber);
        this.blockNumber = web3.utils.toAscii(event.blockNumber);
        console.log("Parse timeStamp: "+event.timeStamp);
...

        
<p>Even though I'm relatively inexperienced with JavaScript, it seems like <code>onreadystatechange is being triggered multiple times as the issue arises on const jsonObj = JSON.parse(jsonString). Nonetheless, I do end up receiving the logs from the subsequent for loop.

If anyone can offer insight into what might be wrong and how to rectify it, that would be greatly appreciated!

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

Can a script be executed using ssh2shell or simple-ssh in node.js?

I recently inquired about the feasibility of initiating a telnet session from a remote SSH session using node.js. Typically, I establish an SSH connection to a Linux server with Putty and then proceed to conduct a telnet session to another idirect/modem. I ...

Tips for achieving the Bootstrap 5 Modal Fade Out effect without using jQuery or any additional plugins apart from Bootstrap

I'm facing some challenges in achieving the fade out effect in Bootstrap 5 modals. I am aiming for something similar to the "Fade In & Scale" effect demonstrated here: https://tympanus.net/Development/ModalWindowEffects/ It is crucial for me to accom ...

What could be the reason for the "begin" script failing to execute?

I have a simple node and express application that works as expected, but I encounter an issue when trying to run the server from the script. When my script is set to "start", running the command simply opens my cmd. However, if my script is set to any oth ...

unable to parse JSON correctly

After attempting to parse a JSON file, I encountered an issue. When following the syntax for parsing, I received an error stating that the string cannot be converted into an array of dictionaries. However, after resolving this problem, it resulted in gener ...

What is the best way to locate a div element with a specific style?

What is the method to locate a div element by its style? Upon inspecting the source code in IE6, here is what I find: ...an><div id="lga" style="height:231px;margin-top:-22px"><img alt="Google"... How can this be achieved using JavaScript? ...

Vuejs Error: "No template or render function defined for a single file component"

When attempting to import all components from a folder and display one based on a passed prop, I encountered an error at runtime. I am using webpack with vue-loader to import all my components, each of which is a *.vue file. The issue arises when importi ...

Executing a function on the window object in JavaScript

I have come across the following code and am seeking guidance on how to get the last line to function correctly. The API I am using currently employs _view appended as its namespacing convention, but I would prefer to switch to something like arc.view.$f ...

Utilizing Puppeteer to Navigate and Interact with Elements Sharing Identical Class Names

I am new to Puppeteer and NodeJs, and I am attempting to scrape a specific website with multiple posts that contain a List element. Clicking on the List element loads the comment section. My question is: I want to click on all the list elements (since th ...

Unable to modify headers after they have already been sent to the client - an error has occurred

I am encountering an issue when trying to redirect the page to another page. Everything works fine with the first post request, but starting from the second one, I keep getting this error message: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after t ...

What is the most effective way to reduce the number of http requests caused by onChange events in Material UI Slider?

Is there a way to optimize my request handling? In my React project, I am currently utilizing Material UI's slider. With the onChange property, I am making HTTP POST requests whenever the slider is moved. Here is a snippet of the code: <Slider ...

modifying the placeholder font and relocating chips/tags beneath the search bar

I'm encountering some challenges with material-ui that I could use assistance with. Firstly, is there a method to have the tags/chips displayed below the search bar instead of above it? Additionally, I've been attempting to italicize and adjust ...

Setting up jsonReader for jqGrid Configuration

I am having trouble displaying data in my Jqgrid. The Json data is coming from a web server, so I am attempting to format it using Jsonreader as a function. Could someone please help me identify any mistakes? Thank you in advance. Here is the code for the ...

The innovative way Vue.js revolutionizes HTML updates within a Websocket onmessage event

I'm new to Vue.js and I'm trying to capture data inside the onmessage event of a websocket and update an HTML component (either a span or div). While console.log or alert functions work in onmessage, I couldn't get it to update the span. Th ...

Angular JS displays an error message when the user selects an input field and fails to provide any input

On my wizard steps application, I have implemented Angular JS validation that displays errors when the user enters and removes a character in the input field. However, I am trying to figure out how to show the error message if the user tabs onto the fiel ...

Encountering a syntax error when using a JSONP callback in AngularJS

Assistance Needed some.factory('homeService', ['$http', function($http){ return { getEvents : function(url) { return $http.jsonp(url); } } }]); Instructions homeService. ...

Vue table displaying a list of books with a button that allows users to easily send the title of the

Hey everyone, I am new to Vue and struggling with a certain task. I have two tables: Books and Booking. Books: ID, NAME, AUTHOR etc. Booking: ID, ID_USER, ID_BOOK I'm creating a page in Vue that displays all bookings, but the table only shows the BOO ...

Verifying StartDate and EndDate using AngularJS and Bootstrap Datepicker

My HTML Coding <form name="myForm"> <div class="row"> <div class="col-md-2"> <input data-ng-model="Data.StartDate" type="text" id="startDate" name="startDate" class="form-control" data-da ...

Bringing in a JavaScript file into a Svelte component

Today, I stumbled upon Svelte and I am really intrigued by the concept. However, I encountered an issue while attempting to import a small helper.js file. No matter what I try, whenever I reference the class, I receive the error: ReferenceError: Helper ...

Obtaining the result from within the .then() block

Through the utilization of Google's API, I am successful in retrieving and displaying nearby places on the console. router.get('/', function (req, res, next) { // Locating nearby establishments googleMapsClient.placesNearby({ ...

Converting a JSONObject to an Array in JavaScript

I am new to JSON and encountering an error while trying to fetch data: E/Volley: com.android.volley.ParseError: org.json.JSONException: Edit: Full StackTrace: 2020-11-10 20:06:48.606 10505-10505/com.madcoderz.jsonparsing E/Volley: com.android.volley.Pars ...