gpt4 book ai didi

javascript - 尝试从 Express 和 Node 应用程序返回 JSON 对象。获取一个空白对象

转载 作者:行者123 更新时间:2023-12-03 01:04:30 24 4
gpt4 key购买 nike

请帮帮我,我觉得它与 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.
});

关于javascript - 尝试从 Express 和 Node 应用程序返回 JSON 对象。获取一个空白对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52470766/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com