XPages [TypeError] Exception at view.getNextDocument(curDoc)

给你一囗甜甜゛ 提交于 2019-12-06 15:52:23

Actually, this works exactly as expected :-)

It has nothing to do with type of variables etc. Basically, if you want to traverse a view you need to set the "starting" point which in your case you do with curDoc = v.getFirstDocument();. Then when you want the next document you use nextDoc = v.getNextDocument(curDoc);, i.e. you tell it to return the document following the curDoc. This is the way the backend objects have been working since LotusScript was introduced back in version 4. And it is the same pattern no matter whether you use LotusScript, SSJS or Java :-)

So a common pattern would be:

var v:NotesView = database.getView("ViewByYear");
var curDoc = v.getFirstDocument();
while(null != curDoc){
  // Do some work with curDoc
  curDoc = v.getNextDocument(curDoc);
}

Now, you may have heard of memory considerations in relation to traversing a view. Basically, Domino will normally take care of memory handling for you. But especially when iterating over large document collections you should use a slightly different pattern (which you can also use for small document collections):

var v:NotesView = database.getView("ViewByYear");
var curDoc = v.getFirstDocument();
var nextDoc = null;
while(null != curDoc){
  nextDoc = v.getNextDocument(curDoc);
  // Do some work with curDoc
  curDoc = null; // reclaim memory
  curDoc = nextDoc;
}

/John

The question has been answered and just wanted to add the recycle bit and I'm sure others can benefit it.

var v:NotesView = database.getView("ViewByYear");
//var curDoc:NotesDocument = doc.getDocument(); // not needed
var curDoc:NotesDocument = v.getFirstDocument(); // starting reference as explained by @John

while (curDoc != null){
  var nextDoc = curDoc.getNextDocument(curDoc); // to fetch the next document
  curDoc.recycle(); // recycle called to manage memory
  curDoc = nextDoc; // replacing/passing data between variables
}

I've modified my answer to utilize NotesViewNavigator:

var v:NotesView = database.getView("ViewByYear");
var curDoc:NotesDocument = doc.getDocument();

// curDoc = v.getFirstDocument(); /* if this line is uncommented the error does not occur */

if (curDoc==null) return(false);
var nav:NotesViewNavigator = v.createViewNavFrom(curDoc);
var entry:NotesViewEntry = nav.getNext();
if (entry == null) { return false; }  // check to see if curDoc is last doc in view
var nextDoc:NotesDocument = entry.getDocument();

Is it possible curDoc is the last doc in the view in your code?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!