onsdag 25 maj 2011

DBColumn might not return what you expect

I found out that @DBColumn despite what the documentation says not always return an Array.
If there is only one entry inside the view you will get an string back.

Is will give you problem if you are using this as the source for an repeat control or somethign elese that is expecting an array.

This is how you fix the problem

function isArray(obj) {
return obj.constructor == Array;
}
var d;
d=@DbColumn("","MyData",1);
if(isArray(d)==false){
d=[d]
}
return d

Happy Coding

3 kommentarer:

  1. Another way... from the Xpages Cheatsheet @ xpagescheetsheet.com is:

    var r = @DbLookup(...);
    return ((r.constructor == Array) ? r : [r]);

    I think Jeremy Jodge originally came up with this.

    SvaraRadera
  2. Did't remember that this was already found.
    So I reinvented. So said I didn't think of the great Cheetsheet instead.

    SvaraRadera
  3. There are several of the @Functions that work inconsistently. To make it easier to work with, I wrote a wrapper function a while back.

    If you wrap all the "problem" function calls with something like this, you don't have to test every call.

    http://dontpanic82.blogspot.com/2010/09/xpages-helper-function-for-inconsistent.html

    SvaraRadera