“undefined” creeping into append - how to debug?

Hi - Completely new to JavaScript here

I’ve got a draft (code name Fragmence) with hundreds of lines structured like this:

Quote Blah blah blah #tag #tag2
Writing Prompt #tag2
What resources can they bring to bear? #question

My script is trying to pull a random line out. But I’m getting the word “undefined” in my append action at the bottom.

var fullFragDraft = Draft.find('51E1E4D4-F56E-415A-808F-C76E8091B71F')

var splitFrag = fullFragDraft.lines

// this is whete I'd like to filter lines based on words inside i.e. include lines with the text "#Modern" and exclude any lines witn the text "#question"

var filteredFrag = splitFrag
var numLines = filteredFrag.length
var RandLine = Math.round(Math.random() * numLines)
var ResultLine = splitFrag [RandLine]

draft.append ("\n\n" + RandLine + "\n" + filteredFrag.length + "\n" + ResultLine + "\n");



What resources can they bring to bear? #question

I’m getting the error even in the simplest case like

draft.append ("\nran\n");

Which outputs


instead of just


I stopped using .append(string) and used

d.content = d.content + Randline

And the “undefined” is gone!

@_ry, I can see the same behaviour in both the append and the prepend Draft functions,

If I have this script.


Then run it against a draft with just three characters of content like this.


I would expect to get this output.


I get this output instead, with mysterious undefined entries.


@agiletortoise, can you reproduce this and confirm they are bugs?

Have you tried explicitly passing the separator argument? The docs say it is optional, but looks like the implementation might disagree. Docs

(will make note to test implementation as well)

Explicitly specifying the separator parameter works as I’d expect. The docs actually seem to reference the defaults as being new lines, which must be what is showing as undefined here.

That’s right - when I specify the second parameter in append it also works.

Docs say it’s optional but I guess it’s required.