No reply from Axios after using async await

Here's a simple method that utilizes Axios to make a call to an API. Interestingly, when the method is called, it doesn't display any output, no logs or error messages.

async deActivate(product: Product): Promise<void> {

    try {
        const response = await this.axios.put(`products/update`, {
                data
            }).then(value => {
            return value.data;
        }).catch(error => {
            console.log(`error: ${JSON.stringify(error)}`);
        });

        return response;
    } catch (e) {
        console.log(e);
    }
}


await deActivate(product)

Answer №1

You have some issues in your code that need to be addressed:

  • Make sure your return type is not void if you are returning data
  • Ensure all code paths return a result
  • If you call await disable(product), there may not be any output without an error
  • Logging should be at the appropriate level
  • Avoid mixing Promise and async/await syntax when possible
  • It's unclear where the data in your example comes from; make sure to specify this or pass the product instead

Consider revising your code like this:

async disable(product: Product): Promise<any> { // Specify expected type here
    try {
        const response = await this.axios.put(`products/update`, {data}); // Avoid mixing Promise and async/await syntax

        return response.data; // Return response data directly
    } catch (e) {
        console.error(`error: ${JSON.stringify(error)}`);
        throw e; // Rethrow error or return null if request fails
    }
}


const result = await disable(product);
console.log(result); // Output result to check what you get

Here are some tips for troubleshooting similar issues in the future:

  • In the browser, check the network tab for sent requests and responses
  • Log extensively or use a debugger to track your code execution

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

Using Vue.js to conditionally render data in a v-for loop

Below is the code snippet I am working with: <article class="project-card" v-for="item in en.projects" Additionally, here are some import statements: import fr from '../assets/datas/fr.json' import en from '../assets/datas/en. ...

NuxtJS using Babel 7: the spread operator persists in compiled files

Struggling to get my NuxtJS app functioning properly on IE11. Despite multiple attempts to configure Babel for compatibility, spread operators are still present in the built pages files, suggesting that Nuxt code is not being transformed correctly. Below ...

What is the best way to iterate through data with ajax and mysql?

I have a school project where I need to create an MP3 album playlist using a premade PHP API with JavaScript or jQuery (without using PHP). I can input the data via an AJAX call. The goal is to add multiple songs along with their URLs into a column named s ...

Are all components in Next.js considered client components by default?

I have created a Next.js app using the app folder and integrated the Next Auth library. To ensure that each page has access to the session, I decided to wrap the entire application in a SessionProvider. However, this led to the necessity of adding the &apo ...

Can React components be saved in an array?

I am currently working on enhancing the code snippet provided below. This code is intended to iterate through elements of an array using keys to locate images within a lengthy SVG file directly embedded in the document under the identifier "SomelongUglySVG ...

When attempting to access /test.html, Node.js server returns a "Cannot GET"

Embarking on the journey of diving into Pro AngularJS, I have reached a point where setting up the development environment is crucial. This involves creating an 'angularjs' directory and placing a 'test.html' file in it. Additionally, o ...

REACT: Implement a feature to add a distinctive border around the currently selected image

When selecting a picture, I would like to have a border around it. Specifically, if out of 6 pictures I choose 3, I want highlighted borders around those selected images. How can this be achieved? EDIT: I am utilizing React to address this issue. ...

Using callback functions to handle parameters in GET requests

Hey there, I'm currently diving into the world of callback functions and I have a burning question that needs clarification. Adding event listeners seems easy enough: $0.addEventListener("click", function(event){console.log(event)}); When you click s ...

Localization of text in jQuery timeago.js

I have implemented J Query time ago to display date and time on my website. I am currently working on a multilanguage website where I want the time ago message to show as "1 min ago" for English users and "1 دقیقه قبل" for Farsi users. Can I achi ...

Personalize the "set up notification" PWA on React

Is it possible to customize this default design, including the picture, title, description, and background? I made changes in manifest.json, but nothing seems to have happened. Here is a picture of the random install prompt that I would like to customize ...

There was a problem retrieving the product information from the API

I have been struggling to pinpoint the exact issue at hand. The foundation of HTML and CSS is pre-written, with JavaScript responsible for generating the core elements to populate the DOM. Within my script, I am attempting to retrieve product data in ord ...

Trouble with database updates in Mongodb using Node.js: issues with ".findOneAndUpdate()" operation

When attempting to update my database using .findOneAndUpdate(), I encountered an issue where the embedded document competitorAnalysisTextData remained empty despite no error messages. // on routes that end in /users/competitorAnalysisTextData // -------- ...

Issues encountered with Three.js MeshBasicMaterial functionality

I am currently working on generating a texture using Three.js. The texture_f1 source I am using is a .png file, which allows the background to show through. The issue arises when attempting to set the background color using color: 0xffffff in conjunction ...

Utilizing the WebSocket readyState to showcase the connection status on the application header

I am currently in the process of developing a chat widget with svelte. I aim to indicate whether the websocket is connected or not by utilizing the websocket.readyState property, which has the following values: 0- Connecting, 1- Open, 2- Closing, 3- Close ...

Using AJAX and jQuery for database connectivity allows for seamless data retrieval and manipulation

Greetings! I am currently facing an issue with AJAX & JQUERY while trying to access my database. After researching online, I found a script that seemed promising for my problem. However, when I attempted to implement it, I encountered difficulties. Using ...

Step-by-step guide to swapping an element with a textarea element using javascript

My current project involves creating a user profile that includes a text box where users can describe themselves. I've already implemented a separate page for editing the profile, but now I want to add a feature where users can hover over their descri ...

Debugging local Messenger: BotFrameworkAdapter cannot find activity type

I have been developing my bot locally using the bot emulator and everything has been working smoothly. Now I am in the process of integrating it with Messenger and trying to run it locally as well. I am attempting to establish a connection from Messenger ...

Clicking within the text activates the dropdown menu, but clicking outside the text does not

My custom drop down menu is not functioning properly. When I click on the text, it successfully links to another place, but when I click beside the text, it does not link. Can you please help me identify what's wrong here? Your assistance would be gre ...

What is the best way to access JSON data that is saved in a variable located within a separate component?

I'm currently utilizing axios to fetch JSON data from my API, mapping it, and storing it as a variable. I'm struggling to figure out the optimal way to call these variables within my React components. Retrieving and Storing JSON Data as Variable ...

Performing a count query with MongoDB Mongoose by grouping data based on multiple fields

I've developed an analytics API using MongoDB. Here is the model for my sessions: const sessionSchema = new Schema( { user: { id: Number, name: String, email: String }, }, { timestamps: true }, ); My goal is to calculate the number of uni ...