TonyW
TonyW

Reputation: 18895

Sails.js: Inserting one-to-many associated models to MongoDB

I am trying to take advantage of the Waterline ORM in Sails.js to build an example app that has a model called 'Category'. Because a category can have multiple sub categories, I have the following one-to-many association for this model:

module.exports = {

  adapter: 'mongo',
//  adapter: 'someMysqlServer',

  attributes: {
    categoryTitle: {
      type: 'string',
      required: true
    },

    parentCat: {
      model: 'category'
    },

    subCategories: {
      collection: 'category',
      via: 'parentCat'
    },

    articles: {
      collection: 'article',
      via: 'category',
      required: false
    }

  }
};

In the CategoryController.js, I have the create method that first tries to see if the new category has a parent category assigned to it; however, I feel the code is quite messy, and the parentCat in Mongodb is always empty even if I tried to assign a parent category in the form submission. So I am wondering if this is the right way to do it:

 create: function(req, res, next) {
        var params = req.allParams();

        // set parent category if exists
        if (params.parentCat) {

            Category.findOne({categoryTitle : params.parentCat})
                .exec(function(err, category) {
                if (err) {
                    return false; //not found
                } else {
                    params.parentCat = category.id;  //found the parent category
                    console.log('parent cat id is: ', category.id);
                }
            });
      }

        Category.create(params, function(err, newCategory) {
            if (err) {
                return next(err);
            } else {
                console.log('new category created');
            }
            console.log('successfully added the category: ' + newCategory.categoryTitle)
            res.redirect('/category');
        }); // create the category
    }

Upvotes: 2

Views: 396

Answers (1)

Ryan W
Ryan W

Reputation: 6173

The issue of your code is the callback.

I created a new version of code with the async feature (which is already in your sails app), hope it will help you.

create: function(req, res, next) {
    var params = req.allParams();

    async.waterfall([
        function(callback) {
            // set parent category if exists
            if (params.parentCat) {
                Category.findOne({
                        categoryTitle: params.parentCat
                    })
                    .exec(function(err, category) {
                        if (err) {
                            return false; //not found
                        }
                        params.parentCat = category.id; //found the parent category
                        console.log('parent cat id is: ', category.id);
                        callback(null, params);
                    });
            } else {
                callback(null, params);
            }
        },
        function(params, callback) {
            Category.create(params, function(err, newCategory) {
                if (err) {
                    return next(err);
                }
                console.log('successfully added the category: ' + newCategory.categoryTitle);
                callback(null, newCategory);
            }); // create the category
        }
    ], function(err, result) {
        console.dir(result);
        res.redirect('/category');
    });
}

Upvotes: 1

Related Questions