I’m writing a server-side plugin using a node package named Axios to load a URL into an object.
I’m using the .then/.catch structure as I saw comments in this forum about implementation issues with the context.async structure.
However, it seems as if none of the code within the .then is being executed.
I base this on my sprinkling of console.log messages through the code.
The code works as a node JS file, but when I copy that code into the plugin action, it’s not working quite the same.
The plugin code is below, and further below that are the server log messages showing the console.log messages that ARE being executed.
If anyone can shed any light, I would be extremely grateful.
=== Plugin code===
function(properties, context) {
let webpage = properties.webpage
console.log(“CustomPlugin:getLinks:Execution starting”)
console.log("CustomPlugin:getLinks:webpage: ", webpage)
const cheerio = require(“cheerio”)
const axios = require(“axios”)
let objectToReturn = {
_p_links: [
{
_p_href: “empty”,
_p_rel: “empty”,
_p_text: “empty”
}
],
_p_returnStatus: 200,
_p_returnStatusText: “OK”
}
console.log("CustomPlugin:getLinks:declared objectToReturn1: ", objectToReturn)
/*
const outputfile = “outputfile.htm”
const fs = require(“fs”)
function write2outputfile (data2write) {
fs.writeFile(outputfile, data2write, function(err) {
if (err) {
console.log("fs err: ", err)
}
}
)
}
*/
axios.get(webpage)
.then(response => {
const $ = cheerio.load(response.data)
objectToReturn._p_returnStatus = response.status
objectToReturn._p_returnStatusText = response.statusText
console.log("objectToReturn._p_returnStatus: ", objectToReturn._p_returnStatus)
console.log("objectToReturn._p_returnStatusText: ", objectToReturn._p_returnStatusText)
console.log("objectToReturn.then1: ", objectToReturn)
/* write2outputfile(response.data) */
const webpagelinks = $("a")
console.log("webpagelinks: ", webpagelinks)
webpagelinks.each(function(){
var link = {
"_p_href": $(this).attr('href'),
"_p_rel": $(this).attr('rel'),
"_p_text": $(this).text()
}
objectToReturn._p_links.push(link)
})
console.log("objectToReturn.then2: ", objectToReturn)
return { "returnedObject" : objectToReturn }
}).catch(function (error) {
if (error.response) {
/* https://www.npmjs.com/package/axios#handling-errors */
objectToReturn._p_returnStatus = error.response.status
objectToReturn._p_returnStatusText = error.response.statusText
console.log("objectToReturn.catch: ", objectToReturn)
return { "returnedObject" : objectToReturn }
}
})
console.log(“CustomPlugin:GetLinks:Execution stopping”)
}
=== server log messages ===
START RequestId: dad3583a-56c5-438e-914d-434dfaba10ee Version: $LATEST 2023-01-23T00:52:48.178Z dad3583a-56c5-438e-914d-434dfaba10ee INFO CustomPlugin:getLinks:Execution starting 2023-01-23T00:52:48.178Z dad3583a-56c5-438e-914d-434dfaba10ee INFO CustomPlugin:getLinks:webpage: https://organicgrowth.biz/link-building/you-do-not-want-backlinks-to-your-landing-pages/ 2023-01-23T00:52:49.920Z dad3583a-56c5-438e-914d-434dfaba10ee INFO CustomPlugin:getLinks:declared objectToReturn1: { _p_links: [ { _p_href: ‘empty’, _p_rel: ‘empty’, _p_text: ‘empty’ } ], _p_returnStatus: 200, _p_returnStatusText: ‘OK’ } 2023-01-23T00:52:50.197Z dad3583a-56c5-438e-914d-434dfaba10ee INFO CustomPlugin:GetLinks:Execution stopping END RequestId: dad3583a-56c5-438e-914d-434dfaba10ee REPORT RequestId: dad3583a-56c5-438e-914d-434dfaba10ee Duration: 2430.35 ms Billed Duration: 2431 ms Memory Size: 128 MB Max Memory Used: 84 MB Init Duration: 376.50 ms