我想从URL获取JSON并将其存储在也可以在其他函数中使用的变量中,但是我不知道该怎么做
我尝试过使用jquery和ajax,但是我不知道如何将jquery添加到节点应用程序
var express = require("express");
var app = express();
var http = require("http");
var getJSON = require('get-json');
app.use(express.static(__dirname + '/public'));
app.get("/", function (req, res) {
res.render("home.ejs");
});
app.get("/website", function (req, res) {
res.render("website_pick.ejs");
});
app.get("/item", function (req, res) {
var web1 = req.query.web1;
var web2 = req.query.web2;
var web3 = req.query.web3;
if (web2 == undefined) {
web2 = '';
}
if (web3 == undefined) {
web3 = '';
}
app.set('web1', web1);
app.set('web2', web2);
app.set('web3', web3);
res.render("item_pick.ejs");
});
app.get("/display", function (req, res) {
var item = req.query.item;
app.set('item', item);
var web1 = app.get('web1');
var web2 = app.get('web2');
var web3 = app.get('web3');
var item_split = item.split(" ");
var item_join = item_split.join('+');
var results_1 = [];
for (var i = 0; i < 9; ++i) {
var result_page = 1 + (10 * i);
var result_url = 'https://www.googleapis.com/customsearch/v1?key=&cx=&num=10&siteSearch=' + web1 + '&siteSearchFilter=i&q=' + item_join + '&start=' + result_page;
getJSON(result_url, function (error, response) {
for (var i = 0; i < 9; ++i) {
var result_item = response['items'][i];
results_1.push([result_item['title'], result_item['link'], result_item['pagemap']['cse_image'][0]['src']]);
}
});
}
res.render("display.ejs", {result_list: results_1,});
});
app.listen(5000);
在getJSON
路由display
的{{1}}块中,正在存储正确的数据,但是在results_1
中,display.ejs
为空
答案 0 :(得分:0)
results_1
为空,因为您在getJSON
完成之前使用了它,并且您将数据放入callback
的{{1}}被执行了
您需要使用result_1
等待所有Promise.all
请求完成,然后呈现getJSON
。
result_1
答案 1 :(得分:0)
我可能会将其重构为一个可以进行回调的函数(如果您不使用promises ...您应该是:))
function getData(callback) {
var results = [];
var errors = [];
var request_count = 9;
for (var i = 1; i <= request_count; ++i) {
var result_page = 10 * i;
var result_url = 'https://www.googleapis.com/customsearch/v1?key=&cx=&num=10&siteSearch=' + web1 + '&siteSearchFilter=i&q=' + item_join + '&start=' + result_page;
getJSON(result_url, function (error, response) {
request_count++;
if (error) {
results.push(error)
} else {
for (var i = 0; i < 9; ++i) {
var result_item = response['items'][i];
results.push([result_item['title'], result_item['link'], result_item['pagemap']['cse_image'][0]['src']]);
}
}
if (i == request_count) {
callback(errors, results);
}
});
}
}
以及基于承诺的版本:
async function getData() {
const request_count = 10;
const requests = [];
for (let i = 0; i < request_count - 1) {
const result_url = 'https://www.googleapis.com/customsearch/v1?key=&cx=&num=10&siteSearch=' + web1 + '&siteSearchFilter=i&q=' + item_join + '&start=' + result_page;
requests.push(new Promise((resolve, reject) => {
getJSON(result_url, (error, response) => {
if (error) return reject(error);
for (let i2 = 0; i2 < 9; ++i) {
var result_item = response['items'][i];
resolve([result_item['title'], result_item['link'], result_item['pagemap']['cse_image'][0]['src']]);
}
})
}))
}
const results = await Promise.all(requests)
return results
}
此代码未经测试,但可以使您朝正确的方向前进。