A
A
Anastasia2021-07-13 12:10:33
Node.js
Anastasia, 2021-07-13 12:10:33

Why is it not possible to get GET request data in request, an empty variable in the console?

const request = require('request');

var cards;

request({
    method: 'GET',
    url: 'https://reqres.in/api/users'    
   }, function (error, response, body) {
   if (!error && response.statusCode == 200) {
    cards = body;
   }
 })

console.log(cards);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2021-07-13
@name4less

const request = require('request');

var cards;

be
On the topic, this is an asynchronous operation, you have the value displayed before its execution, even if it takes very little time, display it after (by pressing a button or in the callback'e of the request function)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question