'session.getDatabase returns null

I'm trying to open my own database via Xpage using JS on beforePageLoad event.

I have this code:

var db:NotesDatabase = session.getDatabase("x", "y",false);
var dbCount = db.getAllDocuments().getCount();

X contains the server name and y the database name. I've tried this with other databases and still got null all the time, I've tried different ways to access the database and still no results. Any suggestions where I might look for the reason why is this happening?



Solution 1:[1]

Very common mistake with hard coded path is missing escape character for backslash, used in file path.

Chceck you path and replace single backslashes \ with double backslashes \\. Another option is to use single slah / - it is valid directory/folder separator on both platforms (Windows and Linux).

I strongly recommend NOT to use hard coded paths in sources. Some form of configuration will solve your problem (properties file, profile document, configuration document, ...) - stored value need not to be escaped.

Solution 2:[2]

Be careful to not put a leading "/"

This is correct :

var mydb:NotesDatabase = session.getDatabase(null, "mydir/mybase.nsf", false);

and also :

var mydb:NotesDatabase = session.getDatabase(null, "mydir\\mybase.nsf", false);

It also work when using "" instead of "null" to target current server.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Frantisek Kossuth
Solution 2 Fabrice Papirnyk