SOLUTION: Error calling method 'xyz' on an object of type 'function [JavaScript Object]'

In XPages, I had a run time error. Specifically:

Error calling method ‘appendItemValue(string, string)’ on an object of type ‘function [JavaScript Object]’

This was from code like:

var docX:NotesDocument=db.CreateDocument;

docX.appendItemValue(“Form”,“FormX”);

I searched around and couldn’t find the problem, so now that I’ve got the solution, I’m sharing. The problem was that I had forgotten the parentheses at the end of CreateDocument. Therefore, I was assigning the JavaScript function of NotesDatabase.CreateDocument (a function is an object in JavaScript) to docX, not executing NotesDatabase.CreateDocument() and returning the output. Easy mistake, easy fix.

So, the error message makes complete sense to me now, but since this was the first time I’ve made this mistake, it took quite a lot of fiddling before realizing what the problem was.

Hopefully this post will help someone out.

The biggest surprise to me from all this is: I had declared docX as a NotesDocument and I thought this was strict datatyping, but apparently server side JavaScript doesn’t pay attention to declared datatypes or else the first line of the above code should have failed because a NotesDocument is not an ancestor of a JavaScript function object.