Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140042

Can not get data from an HTTP request using request.js

$
0
0

Why is it that I can not get the value for my users object in the App module ("users = UserCtrl.getUsers()")?

The log is:

LOG2: LOG3: LOG1: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object] null


This is my app.js:

const Request = require("request");

// Users Controller
const UserCtrl = (function () {

  // Load users from API
  const getUsers = () => {
    let users = '';
    Request.get('XXX', function (err, res, body) {
      if (!err) {
        users = JSON.parse(body).data;
        console.log(`LOG1: ${users}`);
      }
      console.log(err);
    });
    console.log(`LOG2: ${users}`);
    return users;
  }

  // Public methods
  return {
    getUsers: () => getUsers()
  };
})();

const App = (function (UserCtrl, UICtrl) {

  const init = () => {
    let users = ''
    console.log('Initiallizing App');

    // read the data from the API
    users = UserCtrl.getUsers()
    console.log(`LOG3: ${users}`);
  }

  return { init };
})(UserCtrl);

App.init()

Viewing all articles
Browse latest Browse all 140042

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>