What is preventing the use of data post submission in React JS?

After posting data to the navigation, I retrieve it and then use the following code:

.then(data => console.log(data.PathPDF))
.then(data => window.open(data.PathPDF, '_blank', 'noopener,noreferrer'));

While I can successfully retrieve the value in the console with the first line of code, the window.open function generates an error stating "Cannot read property 'PathPDF' of undefined".

Answer №1

.then(data => console.log(data.PathPDF))

The value you return from the .then callback will be the value in the resulting promise. When console.log is used, it returns undefined, which is then passed to the second .then.

To fix this, you can either modify the code to re-return the data:

.then(data => {
  console.log(data.PathPDF);
  return data;
})
.then(data => window.open(data.PathPDF, '_blank', 'noopener,noreferrer'));

Alternatively, you can consolidate it into a single .then like so:

.then(data => {
  console.log(data.PathPDF);
  return window.open(data.PathPDF, '_blank', 'noopener,noreferrer');
})

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

Conceal the Ipad keyboard while still retaining the ability to input text using a customized keyboard

I need to create an input field where users can enter numbers using a custom "keyboard" created with CSS/HTML on the page. This means I don't want the iPad's keyboard to pop up. Most of the solutions I've come across involve removing focus f ...

Connecting the input[date] and Moment.js in AngularJS

For the purpose of formulating a question, I have prepared a simplified example: ... <input type="date" ng-model="selectedMoment" /> ... <script> angular.module('dateInputExample', []) .controller('DateController', [& ...

Angular Resolution Verification

I'm currently working on making HTTP calls in Angular and I want to trigger a different service function if an error occurs. The problem is, no matter what the original service call function returns, the promise always ends up being "undefined". Here& ...

Unable to retrieve array values from an object

Having trouble retrieving values from an array in an object. Looking for some assistance. Below is the JSON parsing code: let userContent = null; try { userContent = JSON.parse(entity.contactform_content); } catch (e) { userContent = String( ...

How can I use jQuery to retrieve the total number of elements in a dropdown list?

I am working with a dropdown list that looks like this: <select id="ddlProjects"> <option value="315">resproject</option> <option value="320" style-"display:inline">newheavn</option> <option value="395" style="di ...

What is the best way to keep my layout component fixed in the Next13 app directory?

I am struggling to develop a custom layout component that can retrieve its own data. Despite adding 'cache: 'force-cache'' to the fetch function, the updated content from my CMS is still being loaded every time I refresh the page. Below ...

Experiencing the "Module not found" issue while incorporating SCSS into React applications

I attempted to apply a SCSS style to my "Logo.js" component, but I am still unable to resolve the error that keeps popping up: ERROR in ./src/components/Logo/Logo.js 5:0-19 Module not found: Error: Can't locate 'logo.scss' in '/Users/a ...

Sending various data from dialog box in Angular 8

I have implemented a Material Design dialog using Angular. The initial example had only one field connected to a single parameter in the parent view. I am now trying to create a more complex dialog that collects multiple parameters and sends them back to t ...

Retrieving data from a parent object within an iframe on a different origin

In order to incorporate a feature on various websites, I am looking to embed an iframe with JavaScript. This iframe should have the ability to interact with the parent object and display or hide certain elements on the webpage. The main HTML file includes ...

Generating div elements dynamically and applying styles

Generating a div dynamically and adding style to it var newDiv = document.createElement('div'); newDiv.setAttribute("id","dynamic-div"); document.body.appendChild(newDiv); // Simulating dynamic ajax content loading $(document).ready(function () ...

Update the content of a div on the WordPress homepage with the click of a button

Hey there, I'm currently working on customizing the Boutique theme for a website. My goal is to add two buttons to the home page that will display different sets of products when clicked. I've been attempting to use the visibility property, but h ...

Certain individuals are experiencing difficulties accessing my Google application

I have created a node.js application with a Google login feature using the following packages: "passport": "^0.3.2" "passport-google-oauth": "^1.0.0" However, I am facing an issue where only a few users are able to access this feature. Below is the code ...

The POST method in XHR encounters issues on IOS when used with the Canvas to Blob script

Observation I've noticed a specific part of the code that's not functioning correctly only on IOS devices. xhr.open('POST', 'upload.php', true); xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded"); var da ...

Using JQuery selectors in conditional statements

In the context of my webpage, clicking on a tag filters and displays corresponding posts. I now need to handle pagination to navigate to the next set of posts. However, I am facing difficulties with the JavaScript if statement in jQuery, where I struggle ...

The attempt to run 'readAsBinaryString' on 'FileReader' was unsuccessful. The first parameter is not the expected type 'Blob'

I am currently working on parsing an xls file. You can find the file by clicking on the following link: However, I am encountering an error that says: 'Failed to execute 'readAsBinaryString' on 'FileReader': parameter 1 is not of ...

Display or hide navigation item based on the success of a JavaScript post operation

Can I hide a nav-item in my navigation bar until I receive a response after submitting a form? The form sends data to an external API and upon successful submission, I want to display options on that specific nav-item. I can easily show the response but I ...

Discovering necessary information by iterating through JSON

Being new to vue js, my goal is to loop through the provided JSON data and check if the required data is present within the file. Here is a snippet of the JSON: [ { "id": "text-5", "widget": "hello", "params": { "0": "section-right", ...

Rotating images with React

I am encountering an issue with implementing an image carousel in React. Previously, it worked perfectly when I was not using React, but now that I am migrating the page, I am facing an error. ** The carousel is located on the home page: ** const Home = ( ...

Unlocking React Event Handlers using Puppeteer

I'm struggling to fully comprehend my request, and I'm seeking clarification. I am currently working on scraping a website using Puppeteer in NodeJS. So far, I have managed to select the necessary element and access its properties. However, I am ...

Combining Key-Value pairs in JavaScript using JSON

I am currently working on parsing a JSON object to extract the key and value combined into a variable. The desired output I want from the provided JSON is: "/" - 7.84 GiB; "/opt" - 4.86 GiB; "/usr" - 4.80 GiB While I can retrieve objects using my code sn ...