XPages SSJS - catching document not found

What is the “official” way of spotting that a document that you’ve just got wasn’t found?

I’ve looked through loads of example code, but can’t spot anyone error checking to see if the document they’ve just tried to find wasn’t found. E.g.

var doc:NotesDocument = database.getDocumentByUNID( LicUNID );

if( doc == ??? ) {

}

What should the ??? be - null, undefined.

In LotusScript you’d simply do :-

if doc is nothing then

msgbox etc

exit sub

end if

My impression is that the contents of the “returned” non-existant doc varies depending on where the code resides. Within a data source for example, failing to find a doc in a different database returns an empty new document, so the obvious way to check for failure is checking that the returned doc’s UniversalId matches the one you used to look for it with.

Your thoughts welcomed as always.

Thanks. Jerry.

Subject: Re: Document not found

null works for me

Subject: I hate XPage development

What am I doing wrong???

Error source

Page Name:/MIPS.xsp

Exception

Error while executing JavaScript computed expression

Script interpreter error, line=8, col=25: Exception occurred calling method NotesDatabase.getView(string) null

Javascript code

1: var db:NotesDatabase = session.getDatabase( applicationScope.get( “AdminServer” ), applicationScope.get( “AdminFilepath” ));

2: if( db == “null” ){

3: return “Admin Database not found.”;

4: }

5: if( db == null ){

6: return “Admin Database not found2.”;

7: }

8: var view:NotesView = db.getView( “lkSystemParameters” );

Subject: Re: null

I think it should be null (without quotes) - though I’m by no means a javascript expert. The only thing I can think is that you’re code is not exiting at the return, that it’s going into the first if statement block but still continuing afterwards. You might nean if…else if…else statement.

A couple of approaches I’d suggest that might help your sanity. try…catch will avoid it just erroring. www.w3schools.com is a great source for checking javascript syntax.

Better than print is openLogXPagea SSJS library that Matt White adapted and can be copied from TaskJam. This logs to the standard OpenLog database available from OpenNTF. I had to comment out line 140 this.getLogDbPath() - the fnction doesn’t seem to exist. After that it worked fine for me and makes it much easier to capture where code is going and why. It should help confirm if the code is going into one or other of the if blocks, and if it’s continuing afterwards.

Subject: print() is your friend…

  • Use print() to output server, path, and db:

print(“AdminServer [”+applicationScope.get( “AdminServer” )+“]”

print(“AdminPath [”+applicationScope.get( “AdminFilepath” )+“]”;

print("Database is "+(db==null ? “AWOL” : db.getName());

These will appear on the server console when your code runs. My dev server spits out ludicrous amounts of console stuff because I haven’t gotten my logging class converted to JavaScript, so it’s the simplest way to debug SSJS.

  • You want to dump server & path because one should never assume the inputs are what you think they are. Best to explicitly verify to avoid headache later.

Hope this helps…

Subject: My only friend

Thanks David. Likewise my console log is full of print statements for the same reason.

I’m specifically not setting the viewScope variables correctly so as to test the error catching. But print() and the actual Stack Trace are spitting out null, and I’m testing for null and “null” but still failing to catch the error. Why, please???

Subject: Re: Document not found

Thanks Paul for your reply, but it’s not just null - it seems to be “null” i.e. with the quotes, whereas undefined seems to not need quotes. Am I correct?