Javascript 正在尝试从Express和Node应用程序返回JSON对象。获取空白对象

Javascript 正在尝试从Express和Node应用程序返回JSON对象。获取空白对象,javascript,html,node.js,http,express,Javascript,Html,Node.js,Http,Express,请帮帮我,我觉得它与res.json()有关。如果我在第一个请求之后调用它,它就可以工作,但在第二个请求之后就不行了。现在实际的应用程序非常基本,它只是从twitter或instagram等社交媒体上刮取一些用户数据,然后返回json对象。谢谢 app.get("/", function(req, res) { let twitterHandle = req.query.twitter; let instagramHandle = req.query.instagram; let

请帮帮我,我觉得它与res.json()有关。如果我在第一个请求之后调用它,它就可以工作,但在第二个请求之后就不行了。现在实际的应用程序非常基本,它只是从twitter或instagram等社交媒体上刮取一些用户数据,然后返回json对象。谢谢

app.get("/", function(req, res) {
  let twitterHandle = req.query.twitter;
  let instagramHandle = req.query.instagram;

  let twitterURL = "https://twitter.com/" + twitterHandle + "?lang=en";
  let instagramURL = "https://instagram.com/" + instagramHandle;

  var json = {};

  console.log(twitterHandle);
  console.log(instagramHandle);

  // The structure of our request call
  // The first parameter is our URL
  // The callback function takes 3 parameters, an error, response status code and the html
  if (twitterHandle != "") {
    request(twitterURL, function(error, response, html) {
      // First we'll check to make sure no errors occurred when making the request
      if (!error) {
        // Next, we'll utilize the cheerio library on the returned html which will essentially give us jQuery functionality
        var $ = cheerio.load(html);

        // Finally, we'll define the variable we're going to capture
        // We'll be using Cheerio's function to single out the necessary information
        // using DOM selectors which are normally found in CSS.
        var twitterFollowers = $(
          "#page-container > div.ProfileCanopy.ProfileCanopy--withNav.ProfileCanopy--large.js-variableHeightTopBar > div > div.ProfileCanopy-navBar.u-boxShadow > div.AppContainer > div > div.Grid-cell.u-size2of3.u-lg-size3of4 > div > div > ul > li.ProfileNav-item.ProfileNav-item--followers > a"
        )
          .text()
          .replace(/\D/g, "");

        var twitterFollowing = $(
          "#page-container > div.ProfileCanopy.ProfileCanopy--withNav.ProfileCanopy--large.js-variableHeightTopBar > div > div.ProfileCanopy-navBar.u-boxShadow > div.AppContainer > div > div.Grid-cell.u-size2of3.u-lg-size3of4 > div > div > ul > li.ProfileNav-item.ProfileNav-item--following > a"
        )
          .text()
          .replace(/\D/g, "");

        // And now, the JSON format we are going to expose

        json[twitterFollowers] = twitterFollowers;
        json[twitterFollowing] = twitterFollowing;

        // Send the JSON as a response to the client
      }
    });
  }
  if (instagramHandle != "") {
    request(instagramURL, function(error, response, html) {
      // First we'll check to make sure no errors occurred when making the request
      if (!error) {
        // Next, we'll utilize the cheerio library on the returned html which will essentially give us jQuery functionality
        var $ = cheerio.load(html);

        // Finally, we'll define the variable we're going to capture
        // We'll be using Cheerio's function to single out the necessary information
        // using DOM selectors which are normally found in CSS.
        var instagramFollowers = "chicken";

        var instagramFollowing = "chicken";

        // And now, the JSON format we are going to expose
        json.instagramFollowers = instagramFollowers;
        json.instagramFollowing = instagramFollowing;

        // Send the JSON as a response to the client
      }
    });
  }
  res.json(json);
});
app.listen(process.env.PORT || 3000);
module.exports = app;

Javascript是异步的,因此在获得调用
request()
的结果的回调之前发送响应。您需要通过
res.json()
或调用另一个函数从回调中发送请求

// get the request here
app.get("/", function(req, res) {
  // do some stuff
  if (foo) {
    // make a request
    request(url1, function(err, res, html) {
      // get the callback with the result
      const json = { foo: 'bar' };

      // call the function to send the response inside the callback
      return res.json(json);
    });
  }
  // don't send response here since it will get called before the callback
  // even though it is further down in the code since it's asynchronous.
});

request()
之类的调用是异步的,因此在填充之前,您将返回
res.json(json)
。我明白您的意思,我现在正在玩res.json()。你认为把它放在哪里最好?我认为把它放在我拥有的位置是最好的,因为我发出请求,存储数据,然后返回对象。把它放在一个函数中,然后从两个
request()
s的回调函数中调用该函数,其中它说“发送JSON”。