First, I wish to say the switch from context.async to the use of Promises is fantastic.
In theory, it means I can mock up my code in Node JS, then make very few changes to reformat it for a plugin.
However, I have a plugin for which I am reformatting to use the await, then, error, syntax, for which something critical is not working, which indicates to me there is something about the await, then, catch syntax I’m not understanding.
Below is the full code of my plugin.
`
async function(properties, context) {
/* References
Return an object: http://forum.bubble.io/t/output-an-object-for-server-side-client-side/59419
Customized error handling in Bubble: https://youtu.be/AITGodtyVyw
Axios error handling: https://www.npmjs.com/package/axios#handling-errors
*/
const axios = require('axios')
const cheerio = require('cheerio')
var webpageUrl = properties.webpage
var linkUrl = properties.link
let objectToReturn = {
_p_link_href: "",
_p_link_rel: "",
_p_link_text: "",
_p_returnStatus: null,
_p_returnStatusText: "",
_p_errorMessage: ""
}
let webpageHtml = ""
let linkv1 = linkUrl
let linkv2 = ""
let regex1 = /\/$/
let regex2 = /$/
if (linkUrl.match(regex1)) {
linkv2 = linkUrl.replace(regex1, "")
}
else if (linkUrl.match(regex2)) {
linkv2 = linkUrl.replace(regex2, "/")
}
await axios.get(webpageUrl)
.then((response) => {
objectToReturn._p_returnStatus = response.status
objectToReturn._p_returnStatusText = response.statusText
webpageHtml = response.data
})
.catch((error) => {
objectToReturn._p_returnStatus = error.response.status
objectToReturn._p_returnStatusText = error.response.statusText
objectToReturn._p_errorMessage = error.message
})
.finally(() => {
const $ = cheerio.load(webpageHtml)
const webpageLinks = $("a")
webpageLinks.each(function () {
if (($(this).attr('href') === linkv1) || ($(this).attr('href') === linkv2)) {
objectToReturn._p_href = $(this).attr('href')
objectToReturn._p_rel = $(this).attr('rel')
objectToReturn._p_text = $(this).text()
}
})
return { "object": objectToReturn }
})
}
`
The problem is the code in the finally section seems to never execute. If I move the return statement so it’s after the finally section, I can see that the code in the then section definitely did execute as the two variables that are set in that code section are set.
However, here is my NodeJS mock up in which I use console.log statements to see what variables are set when, I can see that in this NodeJS mock up, the code in the finally section DOES execute.
`
/*
*/
let webpageUrl = “https://organicgrowth.biz/link-building/you-do-not-want-backlinks-to-your-landing-pages/”
let linkUrl = “https://www.quoleady.com/product-led-content/”
console.log(“CustomPlugin:verifyLink:Execution starting”)
console.log("CustomPlugin:verifyLink:webpageUlr: ", webpageUrl)
console.log("CustomPlugin:verifyLink:linkUrl: ", linkUrl)
const cheerio = require(“cheerio”)
const axios = require(“axios”)
let objectToReturn = {
_p_href: "",
_p_rel: "",
_p_text: "",
_p_returnStatus: null,
_p_returnStatusText: "",
_p_errorMessage: ""
}
let webpageHtml = “”
const outputfile = “outputfile.htm”
const fs = require(“fs”)
function write2outputfile (data2write) {
fs.writeFile(outputfile, data2write, function(err) {
if (err) {
console.log("fs err: ", err)
}
}
)
}
const verifyTheLink = async function verifyLink(webpageUrl, linkUrl) {
let linkv1 = linkUrl
let linkv2 = ""
let regex1 = /\/$/
let regex2 = /$/
if (linkUrl.match(regex1)) {
linkv2 = linkUrl.replace(regex1, "")
}
else if (linkUrl.match(regex2)) {
linkv2 = linkUrl.replace(regex2, "/")
}
console.log("")
console.log("CustomPlugin:verifyLink:linkv1 =", linkv1)
console.log("CustomPlugin:verifyLink:linkv2 =", linkv2)
let temp = axios.get(webpageUrl)
.then ((response) => {
objectToReturn._p_returnStatus = response.status
objectToReturn._p_returnStatusText = response.statusText
webpageHtml = response.data
console.log("\nCustomPlugin:verifyLink:objectToReturn:then: ", objectToReturn)
})
.catch((error) => {
console.log("CustomPlugin:verifyLink:catch:error.response.status: ", error.response.status)
console.log("CustomPlugin:verifyLink:catch:error.response.statusText: ", error.response.statusText)
console.log("CustomPlugin:verifyLink:error.message: ", error.message)
objectToReturn._p_returnStatus = error.response.status
objectToReturn._p_returnStatusText = error.response.statusText
objectToReturn._p_errorMessage = error.message
console.log("\nCustomPlugin:verifyLink:objectToReturn:catch: ", objectToReturn)
})
.finally(() => {
console.log("\nCustomPlugin:verifyLink:objectToReturn:finally1: ", objectToReturn)
const $ = cheerio.load(webpageHtml)
write2outputfile(webpageHtml)
const webpageLinks = $("a")
webpageLinks.each(function () {
if (($(this).attr('href') === linkv1) || ($(this).attr('href') === linkv2)) {
objectToReturn._p_href = $(this).attr('href')
objectToReturn._p_rel = $(this).attr('rel')
objectToReturn._p_text = $(this).text()
}
})
console.log("\nCustomPlugin:verifyLink:objectToReturn:finally2: ", objectToReturn)
})
console.log("\nCustomPlugin:verifyLink:objectToReturn:first: ", objectToReturn)
}
verifyTheLink(webpageUrl, linkUrl)
console.log(“CustomPlugin:verifyLink:Execution stopping”)
`
Does anyone know what I’m doing wrong?