尝试从Express和Node应用返回JSON对象。获取空白对象

时间:2018-09-23 22:24:00

标签: 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 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;

1 个答案:

答案 0 :(得分:1)

Javascript是异步的,因此您要在获取回调之前将响应发送给<table id="Infiletbl" class="table table-responsive table-hover"> <tr> <th>Select</th> <th>Dept_Code</th> <th>File_Code</th> <th>Carton_Code</th> <th>File_Status</th> <th>Dept_File_ID</th> </tr> <tbody> @if (ViewBag.Infilelist != null) { foreach (var item in ViewBag.Infilelist) { <tr> <td><input id="reqflchk" name="filerequestfc" type="checkbox" class="chkCheckBoxId" value="@item.FileIN_ID" /></td> @Html.Raw(@item.File_Status == "IN" ? "disabled" : "") <td>@item.Dept_Code</td> <td>@item.File_Code</td> <td>@item.Carton_Code</td> <td>@item.File_Status</td> <td>@item.Dept_File_ID</td> </tr> } } </tbody> </table> 。您需要通过NSString* text = "•\tSome text for bullet 1.\n" "•\tSome text for bullet 2.\n" "•\tSome text for bullet 3." UIFont* bodyFont = [UIFont preferredFontForTextStyle: UIFontTextStyleBody]; CGFloat indentSize = bodyFont.pointSize; NSMutableParagraphStyle *paraStyle = [[[NSMutableParagraphStyle alloc] init]; paraStyle.alignment = NSTextAlignmentLeft; paraStyle.tabStops = @[ [[[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentLeft location:indentSize options:@{}] ]; paraStyle.defaultTabInterval = indentSize; paraStyle.headIndent = indentSize; paraStyle.firstLineHeadIndent = 0; paraStyle.lineHeightMultiple = 0.85; paraStyle.lineSpacing = 0; paraStyle.paragraphSpacing = bodyFont.lineHeight * 0.25; paraStyle.paragraphSpacingBefore = 0; [atrStr appendAttributedString: [[[NSMutableAttributedString alloc] initWithString:avc.message attributes:@{ NSParagraphStyleAttributeName:paraStyle, NSFontAttributeName: bodyFont, }]]; 或调用另一个函数从回调内发送请求。

request()