Sails.js:插入一对多关联模型到MongoDB

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

我正在尝试利用 Sails.js 中的 Waterline ORM 来构建一个示例应用程序,该应用程序具有一个名为 'Category' 的模型。因为一个类别可以有多个子类别,所以我对这个模型有以下one-to-many association

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
    }

  }
};

CategoryController.js中,我有创建方法,它首先尝试查看新类别是否有分配给它的父类别;但是,我觉得代码很乱,即使我尝试在表单提交中分配父类别,Mongodb中的parentCat也总是空的。所以我想知道这是否是正确的方法:

 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
    }

你的代码问题是回调。

我使用 async 功能创建了一个新版本的代码(它已经在您的 sails 应用程序中),希望它能对您有所帮助。

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');
    });
}