10 Replies Latest reply on Aug 25, 2006 4:38 PM by cheftimo

    Flex/CF project no-go

    cheftimo Level 2
      Hello, gurus who are familiar with Flexstore and/or Phone Selector:

      Partly for study and partly to prepare for a new project, I have undertaken to redo the Flexstore sample app using an access database and ColdFusion CFCs instead of using the source XML file that comes with Flexstore. For this, I have also referred to the Phone Selector sample app, which IS a CF project.

      I have tested the CFC I created outside of Flex and the resulting array yields the right data, but when I try to compile and run the app, it does load and show the Home and Support views, then stops and the Products view never shows any of the products. At that point, this message appears:


      TypeError: Error #1010: A term is undefined and has no properties.
      at DilemmaGames/::productServiceResultHandler()
      at DilemmaGames/__productService_result()
      at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunction()
      at flash.events::EventDispatcher/dispatchEvent()
      at mx.rpc::AbstractService/dispatchEvent()
      at mx.rpc.remoting.mxml::RemoteObject/dispatchEvent()
      at mx.rpc::AbstractOperation/ http://www.adobe.com/2006/flex/mx/internal::dispatchRpcEvent()
      at mx.rpc::AbstractInvoker/ http://www.adobe.com/2006/flex/mx/internal::resultHandler()
      at mx.rpc::Responder/result()
      at mx.rpc::AsyncRequest/acknowledge()
      at ::NetConnectionMessageResponder/NetConnectionChannel.as$37:NetConnectionMessageResponder: :resultHandler()
      at mx.messaging::MessageResponder/result()


      I have spent the last three-four days checking everything thoroughly, but can’t figure out what I’m doing wrong. Maybe someone who understands the above error message can pinpoint the problem right away. I would love to hear any ideas on this.

      Salud,

      Carlos
        • 1. Re: Flex/CF project no-go
          peterent Level 2
          Please post the code to productServiceResultHandler. We'll have a look-see.
          • 2. Re: Flex/CF project no-go
            cheftimo Level 2
            Hello Peter, here is the code (it's the same as in the Flexstore sample):

            private function productServiceResultHandler(event:ResultEvent):void
            {
            // Remote Service returns an ArrayCollection
            var products:ArrayCollection = event.result.catalog.product;
            var temp:ArrayCollection = new ArrayCollection();
            var cursor:IViewCursor = products.createCursor();
            while (!cursor.afterLast)
            {
            var product:Product = new Product();
            product.fill(cursor.current);
            temp.addItem(product);
            cursor.moveNext();
            }
            catalog = temp;
            }

            Thanx, Carlos
            • 3. Re: Flex/CF project no-go
              peterent Level 2
              If you step through this with the debugger, what do you see for the value of event.result.catalog?

              I see that temp = new ArrayCollection(), but what is catalog and where is it defined?

              Also using the debugger, verify that the cursor is being created.
              • 4. Re: Flex/CF project no-go
                cheftimo Level 2
                OK Peter et al, frankly, the debugger might as well be written in Sanscript for me to look at it. But, in the code fragment I posted previously, I put a breakpoint at this line “var cursor:IViewCursor = products.createCursor();”. At that point the debugger variables show:
                products = null
                temp = null
                cursor = null
                product = null
                I know the problem is that ‘product’ does not exist. But let me explain why I used this code, then maybe you can tell me what I am missing.

                I am using the Phone Selector and Flexstore code as a guide to do this. Flexstore data comes from catalog.xml, which has this structure:

                “<catalog>

                <product productId="1">
                <name>Nokia 6010</name>
                <description>Easy to use without sacrificing style, the Nokia 6010 phone offers functional voice communication supported by text messaging, multimedia messaging, mobile internet, games and more</description>
                <price>99.99</price>
                <image>assets/pic/Nokia_6010.gif</image>
                <series>6000</series>
                <triband>false</triband>
                <camera>false</camera>
                <video>false</video>
                <highlight1>MMS</highlight1>
                <highlight2>Large color display</highlight2>
                </product>

                <product productId="2">
                <name>Nokia 3100 Blue</name>
                <description>Light up the night with a glow-in-the-dark cover - when it's charged with light you can easily find your phone in the dark. When you get ………… me to the gaming action.</description> …………..
                ………. <highlight1>Carl Zeiss Optics</highlight1>
                <highlight2>2 megapixel camera</highlight2>
                </product>

                </catalog>“,

                so I am thinking that’s where variables ‘catalog’ and ‘product’ come from. My ‘Catalog.cfc’ returns ‘THIS.catalog’, an array of ‘product’ items. I tested the cfc in a cfm template, did a <cfdump> of the array, which looks fine – it has the same structure as the array returned by ‘Catalog.cfc’ in Phone Selector, so I am sure I got the data right.

                What I don’t know how to do is get that ‘product’ variable defined in the AS in my main mxml file. It looks to me like if the data comes from mxml, this variable gets created automatically, but not so when the data is coming in from a cfc. What am I missing? Anyone who can throw some light on this, please do so; I will be forever grateful.

                Carlos
                • 5. Re: Flex/CF project no-go
                  peterent Level 2
                  I think I may see the problem.

                  When working with XML you have a root node. This is "catalog" in the XML structure. Which means that event.result is this root node. So event.result.product is an XMLList of all of the product nodes.

                  If you do: var test:XML = <list><person name='peter' /><person name='carlos'></list>;

                  The variable test is the root node. So test.person[0].@name is 'peter'. In other words, the rule of thumb is to ignore the root node because the variable is that node.
                  • 6. Re: Flex/CF project no-go
                    cheftimo Level 2
                    Mr. Ent & anyone else that is familiar with Flexstore:

                    I think I understand the XML bit in the previous post, but I still have not been able to figure this out. Here is what I need to get past the point where I am stuck:

                    The result handler in the original Flexstore app, and which I am using:

                    “private function productServiceResultHandler(event:ResultEvent):void
                    {
                    //HTTPService returns an ArrayCollection for nested arrays
                    var products:ArrayCollection = event.result.catalog.product;
                    var temp:ArrayCollection = new ArrayCollection();
                    var cursor:IViewCursor = products.createCursor();
                    while (!cursor.afterLast)
                    {
                    var product:Product = new Product();
                    product.fill(cursor.current);
                    temp.addItem(product);
                    cursor.moveNext();
                    }
                    catalog = temp;
                    }”

                    The Flexstore app uses this service (A):

                    “<mx:HTTPService id="productService" url="data/catalog.xml"
                    result="productServiceResultHandler(event)"/>”

                    In my version of the app using ColdFusion, here is the service (B):

                    “<mx:RemoteObject
                    id="productService"
                    destination="ColdFusion"
                    source="TimoSite.htdocs.dg.CF.Catalog"
                    showBusyCursor="true"
                    result="productServiceResultHandler(event)" />”

                    Flexstore works, but in my app the debugger shows products = null, temp = null, cursor = null and product = null. My cfc is returning an array with all the needed data, but it looks like Flex is not picking up these variables. I have no way to see what (A)is getting back from the XML source file, but what I need is the results of my call to (B) to be the same as what (A) is returning.

                    I hope to hear any ideas on what is missing here or hints about my code.

                    Regards to all,

                    Carlos
                    • 7. Re: Flex/CF project no-go
                      ntsiii Level 3
                      "var products:ArrayCollection = event.result.catalog.product;"

                      That is the line to look at. As Peter said, "catalog" may be the root node, so the reference shuld be: event.result.catalog.product

                      But you need to examine the event.result object in the debugger to determine exactly what its structure is and to determine how to reference its parts.

                      Tracy

                      • 8. Re: Flex/CF project no-go
                        cheftimo Level 2
                        Peterent, ntsiii et al:

                        Please help. I know the problem is with
                        "var products:ArrayCollection = event.result.catalog.product;", but still no luck figuring this out.

                        I have tried three different scenarios:
                        1. Returning a 2D array from CF to Flex, as in the original Flexstore app;
                        2. Returning a 1D array;
                        3. Returning a database query.

                        In all cases, I can see in the debugger that “event.result” ends up with the right data, but I cannot get AS to see the ‘catalog’ variable – it says it’s not defined; if I skip it and do “event.result.product;”, ‘product’ ends up not defined.

                        I think the problem is the introduction of the “products:ArrayCollection”, like Flexstore uses (needed because of the way the products are displayed, creating a cursor, etc.). My CFC was created with the wizard from the AS Class, so I don’t think this is a mapping issue.

                        In Phone Selector, where the code is simpler:

                        public function resultHandler(event:ResultEvent):void
                        {
                        var p1:ArrayCollection = new ArrayCollection();
                        p1.source=event.result as Array;
                        phones = p1;
                        },
                        I recreated the app using a database query instead of “catalog.xml” as a source, and it works beautifully.

                        From all the questions I see in these forums about CF and Flex working together, I know this subject should be of interest to a lot of us.

                        I will be submitting a new post about this with a different “headline”, to try to get more people involved. I have learned a lot about AS/CF, just fighting with this thing, and I think this is a good way for those interested to learn how all this works.

                        Any other ideas about “event.result.catalog.product;”? What am I missing?

                        Regards,

                        Carlos
                        • 9. Re: Flex/CF project no-go
                          cheftimo Level 2
                          Hello, Peterent, ntsiii (how about inlineblue?) et al:

                          I have made a little progress – I never had any luck referring to ‘catalog’ (the root node of the XML source), so I changed this:

                          private function productServiceResultHandler(event:ResultEvent):void
                          {
                          var products:ArrayCollection = event.result.catalog.product;
                          var temp:ArrayCollection = new ArrayCollection();
                          var cursor:IViewCursor = products.createCursor();
                          while (!cursor.afterLast)
                          {
                          var product:Product = new Product();
                          product.fill(cursor.current);
                          temp.addItem(product);
                          cursor.moveNext();
                          }
                          catalog = temp;
                          }

                          to this:

                          private function productServiceResultHandler(event:ResultEvent):void
                          {
                          var products:ArrayCollection = new ArrayCollection();
                          products.source = event.result as Array;
                          var temp:ArrayCollection = new ArrayCollection();
                          var cursor:IViewCursor = products.createCursor();
                          while (!cursor.afterLast)
                          {
                          var product:Product = new Product();
                          product.fill(cursor.current);
                          temp.addItem(product);
                          cursor.moveNext();
                          }
                          catalog = temp;
                          }

                          Now, ‘temp’ is being created and results in an arrayCollection populated by ‘product’ (instances of the Product class). The problem now: it looks like the ‘product.fill’ method of the ‘Product’ class is not doing anything, so all the ‘product’ instances in ‘temp’ end up with their properties being either null (for the strings) or 0 (for the numbers). The cursor is getting created OK, but because ‘fill’ is not working, all the thumbnails come up almost blank – none of the ‘catalog.properties’ appear.

                          The ‘fill’ method of the Product class is very simple (it works fine in Flexstore):

                          public function fill(obj:Object):void
                          {
                          for (var i:String in obj)
                          {
                          this = obj;
                          }
                          }

                          The debugger shows a correctly populated array with the right properties in ‘event.token.result’. I tried:
                          products.source = event.token.result;
                          products.source = event.token.result as Array();
                          products.source = event.token.result as ArrayCollection();
                          and none of those worked.

                          Any ideas on what else to do?

                          Regards to all,

                          Carlos
                          • 10. Re: Flex/CF project no-go
                            cheftimo Level 2
                            OK, all problems are solved. Rather than using the CFC to make an array out of the database query to bring into Flex (I was doing that to emulate Flexstore exactly), I just brought the query into Flex directly as an ArrayCollection, made a few minor changes and now everything is working beautifully.

                            I learned a lot about Flex and AS while trying all the different scenarios, so it was worth it. If anyone wants to try something similar with Flexstore/Phone Selector, I'll be glad to share the experience.

                            Thanks, everyone, for the help.

                            Carlos