undefined
is actually not an error here but just the result of running the script. Since it does nothing …
As to your question: The function working on the current selection looks like the one below. It’s of course not very elegant to hard-wire the target folder, instead it could ask the user for it, at least in the stand-alone script.
(() => {
const externalFolder = "Posix/Path/To/Folder";
const app = Application("DEVONthink 3");
app.selectedRecords().forEach(r => app.export({record: r}, {to: externalFolder}));
})()
There’s also this