2013-02-14 49 views
1

我真的很喜欢用几乎所有的语言来理解范围和其他本质的东西。现在我正在构建一个快速应用程序,它接受用户输入,然后查询任意api,然后将其提供给控制台。为了处理其余的API,我正在使用Shred。我知道我可以使用get请求中建立的节点,但由于某种原因,我永远无法使它工作。用户向我的应用程序发出以下请求,/ query?query =。这是我现在拥有的。我无法真正描述我在做什么,所以请阅读代码评论。有关范围,node.js和express的问题

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' 
    }); 
*/ 
}; 

我试着这样做

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' 
    }); 
*/ 
}; 

对不起,我的问题不好解释。有人可以帮助或解释发生了什么。

回答

0

所以你认为你需要将数据移出你的嵌套范围,但事实恰恰相反。在这里您可以访问你的上游JSON响应嵌套的作用域,你需要访问res对象,虽然它发送:

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

然而,长期的,你需要做一些更多的节点教程和重点如何使用回调来避免深度嵌套的函数范围。

+0

是的,我可能会切换到不同的休息api,如https://github.com/mikeal/request,因为它处理范围更好。但是你说的是我可以使用.send以某种方式逃避范围,对吧? – aarocka 2013-02-14 12:52:36

+0

你说“逃避范围”的方式好像你还没有理解异步编程如何工作的“啊哈”时刻。这与节点的httpclient vs request与superagent或其他任何事情无关,这就是节点编程的过程。您可以使用函数参数,闭包,回调和流量控制范例(可能通过支持库,如async.js或promises)。当IO完成时,它不会同步并阻塞,IO的结果显示在同一范围内。这完全是一个不同的范例。 – 2013-02-14 18:00:40