0

I am trying to get all users items from steam, so i need to use request module to get json file from steamcommunity. The problem is i don't know how to return that json to app.js and pass through routes.

var request = require('request');

var userItems = function(req, res){
    var userItems = request('http://steamcommunity.com/profiles/'+req+'/inventory/json/730/2', function (error, response, body) {
      if (!error && response.statusCode == 200) {

        var info = JSON.parse(body);
        return res(info);

      }
    });

}

module.exports.userItems = userItems;

Idea is next:

app.get('/account/:id', ensureAuthenticated, ensureID, function(req, res){
  res.render('account', { 
    user: req.user,
    items: userItems(req.params.id, res)
  });
});

I am begginer, and i will learn from start, but for now i just need to work because i am short with the time. Thank you.

Marcs
  • 3,768
  • 5
  • 33
  • 42

1 Answers1

-2

userItems is an asynchronous operation, all code that uses the result of it must be in a callback. Try this:

app.get('/account/:id', ensureAuthenticated, ensureID, function(req, res){
  userItems(req.params.id, function(info) {
    res.render('account', { 
      user: req.user,
      items: info
    });
  });
});

I think the next step should be error handling.

Tamas Hegedus
  • 28,755
  • 12
  • 63
  • 97