对节点回调函数的功能以及如何访问中的数据感到困惑



我有API的启动代码,它允许我搜索术语,如Apple,并将JSON对象列表打印到控制台。

var params = {
q: "Apple",
tbm: "isch",
ijn: "0"
};

var callback = function(data) {
console.log(data["images_results"]);
};

src.json(params, callback);

我想将数据["images_results"]项保存到变量中,而不是打印到控制台。到目前为止,我已经尝试在回调函数中将var设置为data["images_results"](然而,var是在函数外初始化的(,在另一种情况下,我在之后将其设置为src.json(params,callback(。但当我尝试访问该变量时,它显示为未定义。如何使用此API访问数据["images_results"],而不是将结果打印到控制台?

var a;
var params = {
q: "Apple",
tbm: "isch",
ijn: "0"
};

var callback = function(data) {
a = data["images_results"];
};

src.json(params, callback);
console.log(a);

更新


const params = {
engine: "google",
google_domain: "google.com",
hl: "en",
gl: "us",
device: "desktop",
tbm: "isch",
};
const getJson = async () => {
return new Promise((resolve) => {
src.json(params, resolve);
});
};
const getFirstImages = async () => {
const imres = [];
for (item in items) {
params.q = item['sku'];
const json = await getJson();
imres.push(json.images_results[0]["original"]);
}
return imres;
};
getFirstImages().then(imres => {
console.log(imres);
images = imres;
myCache.set('total_images', images);
})

您不能以这种方式保存SerpApi的结果,因为.json方法是异步的,并且callback函数在控制台中打印a后运行。您只需要在某些函数中处理结果,因为您不能在全局范围中使用类似await的东西。如果你告诉我更多关于这些结果你下一步想做什么,我可以更好地帮助你。但现在你可以做这样的事情:

var params = {
q: "Apple",
tbm: "isch",
ijn: "0"
};

var callback = function(data) {
var a = data["images_results"];

// do what you want to do next in this function
};

src.json(params, callback);

或者您可以使用.then链接:

const params = {
q: "Apple",
tbm: "isch",
ijn: "0"
};
const getJson = () => {
return new Promise((resolve) => {
src.json(params, resolve);
});
};
getJson().then(({images_results}) => {
var a = images_results
// do what you want to do next in this function
});

更新时间:

如果我理解正确,你需要做一些类似的事情(查看在线IDE(:

const SerpApi = require("google-search-results-nodejs");
const src = new SerpApi.GoogleSearch(process.env.API_KEY); //your api key from serpapi.com
const params = {
engine: "google",
google_domain: "google.com",
hl: "en",
gl: "us",
device: "desktop",
tbm: "isch",
};
const searchQueries = ["apple", "cherry", "banana"];
const getJson = async () => {
return new Promise((resolve) => {
src.json(params, resolve);
});
};
const getFirstImages = async () => {
const images = [];
for (query in searchQueries) {
params.q = query;
const json = await getJson();
images.push(json.images_results[0]);
}
return images;
};
getFirstImages().then(images => {
console.log(images)
// or do what you want to do next
})

请注意,我们在异步函数getFirstImages中用图像填充数组,等待(await(接收带有结果的对象(json(。另一个注意事项是,不能使用数组方法(例如forEach(来迭代searchQueries,因为await不起作用。

输出:

[
{
"position":1,
"thumbnail":"https://serpapi.com/searches/62e7affb9bddf73501bfde87/images/d6db51527c0e83ba92158999bf4cd1063f03d4fcbdfbcca0bb5f77b191b27a53.png",
"source":"en.wiktionary.org",
"title":"0 - Wiktionary",
"link":"https://en.wiktionary.org/wiki/0",
"original":"https://upload.wikimedia.org/wikipedia/commons/thumb/6/6e/Z%C3%A9ro.svg/1200px-Z%C3%A9ro.svg.png",
"is_product":false
},
{
"position":1,
"thumbnail":"https://serpapi.com/searches/62e7affed737d7249d50f729/images/920c4e458460303dc0bb77ec3f448f9ad3946e5412eb72ca3cbb8751a8cee7c6.png",
"source":"en.wiktionary.org",
"title":"1 - Wiktionary",
"link":"https://en.wiktionary.org/wiki/1",
"original":"https://upload.wikimedia.org/wikipedia/commons/thumb/0/08/Un1.svg/1200px-Un1.svg.png",
"is_product":false
},
{
"position":1,
"thumbnail":"https://serpapi.com/searches/62e7b0064055aa23fb85ce10/images/4036a5ed1a2cf55b31ca73fd5c660dc71431f02052cf7ee6f4796043060b475c.png",
"source":"en.wiktionary.org",
"title":"2 - Wiktionary",
"link":"https://en.wiktionary.org/wiki/2",
"original":"https://upload.wikimedia.org/wikipedia/commons/thumb/3/3b/Deux.svg/1200px-Deux.svg.png",
"is_product":false
}
]

最新更新