1

I really suck at understanding scopes and other things of that nature in just about every language. Right now I am building an express application that takes user input and then queries an arbitrary api and then feeds it to the console. To handle the rest api, I am using shred. I know I can use nodes built in get request, but for some reason, I could never get it to work. The user makes the following get request to my app, /query?query=. This is what I have now. I can't really describe what I'm doing so pleas read the code comments.

var http = require('http');
var Shred = require("shred");
var assert = require("assert"); 

exports.query = function(req, res){
    //thequery is the string that is requested
    var thequery = req.query.query;
    var shred = new Shred();


    console.log("user searched" + " " + thequery);
    console.log();

    //The if statement detects if the user searched a url or something else
    if (thequery.indexOf("somearbitratyrestapi.com") !== -1){
        console.log("a url was searched");
        //find info on the url

        var thedata = shred.get({
          url: "http://somearbitratyrestapi.com/bla/v2" + thequery,
          headers: {
            Accept: "application/json"
          },
          on: {
            // You can use response codes as events
            200: function(response) {
              // Shred will automatically JSON-decode response bodies that have a
              // JSON Content-Type

              //This is the returned json
                  //I want to get this json Data outside the scope of this object
              console(response.content.body);

            },

            // Any other response means something's wrong
            response: function(response) {
             console.log("ohknowz");
            }
          }
        });

            //I want to be able to see that json over here. How do?


    }else{
        console.log("another thing was searched");
    }
/*

    res.render('search-results', { 
        result: 'you gave me a url',
        title: 'you gave me a url' 
    });
 */
};

I tried doing this

var http = require('http');
var Shred = require("shred");
var assert = require("assert"); 

exports.query = function(req, res){
    //thequery is the string that is requested
    var thequery = req.query.query;
    var shred = new Shred();
    //I created a variable outside of the object
    var myjson;


    console.log("user searched" + " " + thequery);
    console.log();

    //The if statement detects if the user searched a url or something else
    if (thequery.indexOf("somearbitratyrestapi.com") !== -1){
        console.log("a url was searched");
        //find info on the url

        var thedata = shred.get({
          url: "http://somearbitratyrestapi.com/bla/v2" + thequery,
          headers: {
            Accept: "application/json"
          },
          on: {
            // You can use response codes as events
            200: function(response) {
              // Shred will automatically JSON-decode response bodies that have a
              // JSON Content-Type

              //This is the returned json
                  //I set myjson to the returned json
              myjson = response.content.body

            },

            // Any other response means something's wrong
            response: function(response) {
             console.log("ohknowz");
            }
          }
        });

            //Then I try to output the json and get nothing
            console.log(myjson);


    }else{
        console.log("another thing was searched");
    }
/*

    res.render('search-results', { 
        result: 'you gave me a url',
        title: 'you gave me a url' 
    });
 */
};

Sorry for the bad explanation of my problem. Can someone please help or explain what is going on.

aarocka
  • 11
  • 1

1 Answers1

0

So you think you need to move data out of your nested scope, but the opposite is true. Within the nested scope where you have access to your upstream JSON response, you need to access the res object and send it though:

myjson = response.content.body
res.send(myjson);

However, long term you'll need to do some more node tutorials and focus on how to use callbacks to avoid deeply nested function scopes.

Peter Lyons
  • 142,938
  • 30
  • 279
  • 274
  • Yea well I might be switching to a different rest api like https://github.com/mikeal/request because it deals better with scopes. But what you are saying is that I can use .send to escape the scope in a way, right? – aarocka Feb 14 '13 at 12:52
  • The way you say "escape the scope" seems like you haven't yet had the "ah ha" moment of comprehension for how asynchronous programming works. It's not so much a matter of node's httpclient vs request vs superagent or whatever, this is how programming in node works, period. You work with function arguments, closures, callbacks, and flow control paradigms (maybe via a supporting library like async.js or promises). It's not synchronous and blocking with results of IO showing up in the same scope when the IO is done. It's just a different paradigm entirely. – Peter Lyons Feb 14 '13 at 18:00