Welcome, Guest. Please login or register.
Did you miss your activation email?
09 Sep 2010, 17:21:09 UTC
Forum home
+  flexdeveloper.eu Forum
|-+  Flex and ActionScript 3.0
| |-+  ActionScript 3.0 (Moderators: JMWhittaker, Jan K, thewarpedcoder, James)
| | |-+  Single line service results causing an error
« previous next »
Pages: [1] Print
Author Topic: Single line service results causing an error (Read 1018 times)
flexy
flexdeveloper.eu
Guru/Addict FD
*****
Posts: 3,209


Recovering Coffee Addict & Adobe Expert


WWW
« on: 18 Nov 2007, 16:06:12 UTC »

So you have a HTTPService result with 2 or more objects in there and everything works fine. Then, you test the same service with only 1 result being returned and you get:

Code:
Error #1034: Type Coercion failed: cannot convert mx.utils::ObjectProxy@ ... to mx.collections.ArrayCollection.

This is the 'single result from HTTPService breaks my app' problem!

The problem is that when only one result is returned, there is only one object, so it isn't cast as an ArrayCollection. When you try to iterate through your result set, your script is trying to access nodes on the object rather than accessing objects in an ArrayCollection.

Using a quick bit of run-time type checking, we can make sure we always get an ArrayCollection. The following function accepts you result object, checks whether it's typed as an ObjectProxy or not, then ensures an ArrayCollection is always returned.

Code:
private function checkResultType( d:Object ) :ArrayCollection
{
/* Force ArrayCollection conversion for single result */
if( d is ObjectProxy )
{
return new ArrayCollection( [ d ] );
}
else
{
return d as ArrayCollection;
}
}
Logged

Pages: [1] Print
« previous next »
Share this on: Twitter Twitter Del.icio.us del.icio.us Digg Digg
Jump to:

©2006-2010 Flexdeveloper.eu/Jodie O'Rourke. All rights reserved.
Adobe®, Adobe® Flash™, Adobe® AIR™ and Adobe® Flex™ are registered trademarks of Adobe Systems Incorporated in the United States and other countries. All rights reserved.

Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC