I’m completely new to JS/Node.js but so far I got this notebook/endpoint working fairly well:
However, since I’m learning as well, I wanted to use the SerpAPI properly, so I wrote this notebook:
that’s NOT working. It gives:
{
"schemaVersion": 1,
"label": "citations",
"message": "[object Promise]", // <--- it should be 1412 here
"color": "orange",
"cacheSeconds": 2592000
}
Here’s my endpoint code that I’d like make it work:
const SerpApi = require('google-search-results-nodejs');
const search = new SerpApi.GoogleSearch(process.env.api_key);
function promisifiedGetJson(params) {
return new Promise((resolve, reject) => {
try {
search.json(params, resolve)
} catch (e) {
reject(e)
}
})
}
async function main(params) {
try {
// `const data = await fn()` is same as `fn().then(data)`
const data = await promisifiedGetJson(params);
const total = data.organic_results[0].inline_links.cited_by.total;
// console.log(total);
return total;
} catch (error) {
console.error("there was an error:", error);
}
}
let params = {
engine: "google_scholar",
as_ylo: "2012",
as_yhi: "2012",
q: '"ACPYPE-Antechamber python parser interface"',
hl: "en",
};
const tot = main(params);
const obj = { "schemaVersion": 1, "label": "citations", "message": tot.toString(), "color": "orange", "cacheSeconds": 2592000 };
exports.endpoint = function (request, response) {
response.end(JSON.stringify(obj));
};
What am I be missing here please?