DomParser in Lambda functions always not defined

Options
I am trying out the Lambda Functions for an HTML transformation, and am using the DOMParser 
However I get "DOMParser is not defined" as an error. i tried two options

a basic one with no success:
var htmlDoc = new DOMParser().parseFromString("","text/html");

and my code which throws same error:
var parser = new DOMParser();
var sourceData = $var.sample;
var htmlDoc = parser.parseFromString(sourceData, 'text/xml');

regardless of what I do, I get the not defined error. Any thoughts would be appreciated.

Comments

  • Ray Deck
    Ray Deck Trusted Xano Expert ✭✭✭
    Options
    DOMParser is provided by on a browser, but not by nodejs, which is what the Xano lambda runs on. So it's not available out of the box.  Preloaded libraries are loaded into global scope and listed on the documentation: https://docs.xano.com/working-with-data/lambdas-javascript

    There are node-side packages (e.g. jsdom) that solve this problem, but the lambda functionality is a little locked down at the moment - loading arbitrary libraries sometimes works with hack-y approaches like loading webpacked javascript into the lambda, or loading external javascript via a fetch request and evaluating it. (I've made the UI cry a bit with these hacks). Glad to give such an end-run a try with jsdom to see if that helps you: https://www.npmjs.com/package/jsdom
  • Vlad B
    Vlad B Member
    Options
    Hi Ray, thank you for your reply! I am pondering if I am going to try and use workarounds or just do it outside Xano for the moment. Your approaches, while detailed, are as you said "hacks", and I am trying to stay away from this as much as possible.