0

I have seen this answer and have created a solution to match my needs. However, just for fun, I decided to make another variation without async and start my descent into callback hell. Here's the async version

     async.auto
        (
            {
                restaurant: function(cb)
                {
                    Restaurant.findOne
                    (
                        {
                            where:
                            {
                                owner: req.session.owner.id,
                                id: req.params.id
                            }
                        }
                    )
                    .populate('categories')
                    .exec(cb);
                },
                categoryItems:
                [
                    'restaurant',
                    function(cb, results)
                    {
                        Item.find
                        (
                            {
                                where:
                                {
                                    category: _.pluck(results.restaurant.categories, 'id')
                                }
                            }
                        )
                        .exec(cb);
                    }
                ],
                map:
                [
                    'categoryItems',
                    function(cb, results)
                    {
                        var categoryItems = _.groupBy(results.categoryItems, 'category');
                        var restaurant = results.restaurant.toObject();
                        restaurant.categories = restaurant.categories.map
                        (
                            function(category)
                            {
                                category.items = categoryItems[category.id];
                                return category;
                            }
                        );
                        return cb(null, restaurant);
                    }
                ]
            },
            function finish(err, results)
            {
                if(err)
                {
                    res.serverError(err);
                }
                else
                {
                    res.json(_.cloneDeep(results.map));
                }
            }
        );

and here's the callback hell version.

     Restaurant.findOne
        (
            {
                where:
                {
                    owner: req.session.owner.id,
                    id: req.params.id
                }
            }
        )
        .populate('categories')
        .exec
        (
            function(err, restaurant)
            {
                if(err)
                {
                    res.serverError(err);
                }
                else if(typeof restaurant === 'undefined')
                {
                    res.notFound();
                }
                else
                {
                    if(typeof restaurant.categories === "undefined" || restaurant.categories === null || restaurant.categories.length === 0)
                    {
                        sails.log.info("i got nuthin")
                    }
                    else
                    {
                        Item.find
                        (
                            {
                                where:
                                {
                                    category: _.pluck(restaurant.categories, 'id')
                                }
                            }
                        )
                        .exec
                        (
                            function(err, items)
                            {
                                if(err)
                                {
                                    res.serverError(err);
                                }
                                else if(typeof items === 'undefined' || items === null || items.length === 0)
                                {
                                    sails.log.info(':(');
                                }
                                else
                                {
                                    var categoryItems = _.groupBy(items, 'category');
                                    var restaurant = restaurant.toObject();
                                    restaurant.categories = restaurant.categories.map
                                    (
                                        function(category)
                                        {
                                            category.items = categoryItems[category.id];
                                            return category;
                                        }
                                    );
                                    res.json(restaurant);
                                }
                            }
                        );
                    }
                }
            }
        );

However, I'm getting TypeError: Cannot call method 'toObject' of undefined from var restaurant = restaurant.toObject(); Why? Why am I not able to access the restaurant variable? I could access it just fine for the where clause.

Community
  • 1
  • 1
No_name
  • 2,732
  • 3
  • 32
  • 48

1 Answers1

0

I am not so smart. var restaurant = restaurant.toObject(); should be restaurant = restaurant.toObject();

No_name
  • 2,732
  • 3
  • 32
  • 48