Hello, I’m trying to get jsdom working to get a value from a fetched web page.
// Filename: backend/counterModule.jsw
import { fetch } **from** 'wix-fetch' ;
import { jsdom } **from** 'jsdom' ;
**const** { JSDOM } = jsdom;
export function pet_counter() {
fetch( "some url" , { "method" : "get" })
.then( **function** (response) {
**if** (response.ok) {
**let** t = response.text();
**return** t;
} **else** {
**return** Promise.reject( "Fetch did not succeed" );
}
})
.then( **function** (txt) {
**const** dom = **new** JSDOM(txt);
**const** script = dom.window.document.querySelector( 'script' );
**const** objJSON = script.textContent.match(/window.changeTargetingData = ([^]+);/)[ 1 ];
**const** obj = JSON.parse(objJSON);
**const** rep = obj.petition.signatureCount.total;
console.log(rep);
**return** (rep);
})
. **catch** ( **function** (err) {
**return** ( 'Failed to fetch page: ' , err);
});
}
But even the basic JSDOM example doesn’t work :
import { jsdom } **from** 'jsdom' ;
**const** { JSDOM } = jsdom;
const domm = new JSDOM(`<!DOCTYPE html><p>Hello world</p>`);
console.log(domm.window.document.querySelector("p").textContent); // "Hello world"
I get :
Error loading web module backend/counterModule.jsw: Cannot destructure property `JSDOM` of 'undefined' or 'null'.
I am confused…