ReferenceError: Api is not defined

DocumentBuilder version: 8.0.0 (according to the website I downloaded it from)
Installation method: download arm.64deb package from Download ONLYOFFICE Document Builder | ONLYOFFICE
OS: Ubuntu (running in docker on mac)

Not sure what I’m missing, I’ve downloaded an installed the prebuilt package from the above link, but trying to run some sample code (below) is giving me the ReferenceError: Api is not defined, not sure if i’ve missed some setup/installation procedure?

script.docbuilder

builder.CreateFile('docx');
let oDoc = Api.GetDocument(0);
let oPar = oDoc.GetElement(0);
oPar.AddText(Argument['key']);
oPar.AddLineBreak();
oPar.AddText(Argument['key2']);
builder.SaveFile('docx', 'simple.docx');
builder.CloseFile();

Running:

/opt/onlyoffice/documentbuilder/docbuilder '/project/script.docbuilder' 
ReferenceError: Api is not defined
    at <anonymous>:1:12

UPDATE

Ended up modifying a different sample script and had It run without failure:

builderJS.OpenFile('path_or_url', '/project/sample.docx');
builderJS.CreateFile('docx');
builderJS.SetTmpFolder('/project/tmp');
builderJS.SaveFile('docx', '/project/sample_update');
builderJS.CloseFile();

where /project/ is a volume mount to my current working dir where i’m running the docker container from.

So I suppose the Api object is not available, still struggling to comprehend the docs on this one…

UPDATE 2

So looks like I may have missed the difference between builder and builderJS, it also seems like the Api is available after I’ve opened/created a file. Seems simple now, but this was a struggle to comprehend

Hello @Jamesplxs,
I’m glad you’ve managed to resolve the issue. Do I understand it right that when you use builder.OpenFile instead of builderJS.OpenFile, the script is not executed?

Using this small script I was getting the same error TypeError: Cannot read property 'GetDocument' of null

builder.OpenFile("/tmp/tmp.test.docx");
var oDocument = Api.GetDocument();
var oParagraph;
builder.SaveFile("docx", "/tmp/test.docx");
builder.CloseFile();

it turned out that the json passed as input to docbuilder --argument=.... was mallformed

Hello @dsl400
Please clarify if you are still facing an issue. If so, please show us how exactly you are passing the script to the DocBuilder.

Hi @Alexandre,
I was testing docbuilder to understand how it works and I was passing the input via command line

docbuilder --argument='invalid-json' /tmp/test.docbuilder
docbuilder -version
v8.1.1.26
sdk version:  8.1.1 (build:26)

Ah, so it was just a test with incorrect command line arguments. Please accept my apologies for the misunderstanding, I thought there was an issue.

I’m really sorry to bring bad news to you but this is an issue. :upside_down_face:
The error reported by the docbuilder when it receives an invalid json as argument is misleading

TypeError: Cannot read property 'GetDocument' of null

the developer will waste hours … maybe days to figure this one out.

Dear @dsl400
Your provided earlier docbuilder script doesn’t contain variables at all. Please pay attention to link in my previous reply that contains a sample with following lines:

var sCompany   = Argument["company"];
var sProduct = Argument["product"];
var sCompatibility    = Argument["compatibility"];

If you are going to use arguments line, please make sure that the script contains necessary variables.

My message does not seam to point you in the right direction.

Currently when it receives an invalid json docbuilder throws an error about Api being undefined.

docbuilder should report an error about json being invalid if the value received under argument is not a valid json.

This will help the developers.

The curent behaviour is confusing and mite cause developers to waste time

Ah, now I get it. Thank you, we are checking the situation.

Hello @dsl400
We agree that output information isn’t informative in the described scenario. We have started working on it. I will update this thread once we have something to share.