K
K
Konstantin2022-03-02 10:35:09
JavaScript
Konstantin, 2022-03-02 10:35:09

How to write the response of a post request to a variable?

I use the axios library on node js

I send a post request, I want to write the answer to a variable and use it outside the request, but I get Promise { <pending> }

How to get the answer and write it to a variable?

const createProduct = api.post('products', newProduct).then(function (response) {
            return response.data;
          });

          console.log(createProduct); //Promise { <pending> }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2022-03-02
@gradk

Read about async in javascript, promises and async/await.
This question is asked on the Toaster several times a day.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question