views:

209

answers:

2

hello, i get this error when i retrieve an XML that only has 1 node (no repeating nodes) and i try to store in an ArrayCollection. -When I have MORE than 1 "name" nodes...i do NOT get an error. My test show that XMLListCollection does NOT work either.

TypeError: Error #1034: Type Coercion failed: cannot convert "XXXXXX" to mx.collections.ArrayCollection.

this error occurs as the line of code:

myList= e.result.list.name;

Why can't ArrayCollection work with a single node? I'm using this ArrayCollection as a dataprovider for a Component -is there an alternative I can use that will take BOTH single and repeating nodes as well as work as a dataprovider? Thanks in advance!

code:

[Bindable]
private var myList:ArrayCollection= new ArrayCollection();

        private function getList(e:Event):void{

            var getStudyLoungesService:HTTPService = new HTTPService();
            getStuffService.url = "website.com/asdf.php";
            getStuffService.addEventListener(ResultEvent.RESULT, onGetList);
            getStuffService.send();

        }

        private function onGetList(e:ResultEvent):void{

            myList= e.result.list.name;
        }
A: 

Hey,

the problem here is, that if you only have one row Flex will complain since it cannot use the result as arrayCollection.

My work around was, that you put number of rows into your XML with the data you want to return: for example I did:

<list><nr_rows>3</nr_rows><name>...</name><name>...</name><name>...</name></list>

So when I get the result back I do a check of how many rows I am getting with (You can get the number of rows returned from MySQL query with mysql_num_rows)

e.result.list.nr_rows

So if this is one, you add Object to the arrayCollection, if there are more than one you can just use result and equal it to AC (in this case projects is AC):

if (event.result.list.nr_rows == '1'){
    myList.addItem(event.result.list.name);
} else {
    myList = event.result.list.name;
}
Ladislav