1

私は、ほぼすべての言語でスコープやその性質の他のものを理解することに本当に夢中です。現在、ユーザー入力を受け取り、任意のAPIをクエリして、それをコンソールにフィードするエクスプレスアプリケーションを構築しています。残りのAPIを処理するために、私はシュレッドを使用しています。get requestに組み込まれているノードを使用できることはわかっていますが、何らかの理由で、それを機能させることができませんでした。ユーザーは、私のアプリ/ query?query=に対して次のgetリクエストを行います。これは私が今持っているものです。私がしていることを本当に説明することはできないので、コードのコメントを読んでください。

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

私の問題の悪い説明でごめんなさい。誰かが何が起こっているのか助けたり説明したりできますか?

4

1 に答える 1

0

したがって、ネストされたスコープからデータを移動する必要があると思いますが、その逆です。resアップストリームの JSON 応答にアクセスできるネストされたスコープ内で、オブジェクトにアクセスして送信する必要があります。

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

ただし、長期的には、さらにノードのチュートリアルを実行し、コールバックを使用して深くネストされた関数スコープを回避する方法に焦点を当てる必要があります。

于 2013-02-14T02:54:17.973 に答える