SailsJS反向破坏已创建的模型失败

时间:2014-04-10 22:02:19

标签: sails.js

假设我有2个模型(一对多);中心模型(一) - 房间模型(很多)。

创建中心时,会在Center.Create回调中创建一系列房间。 如果房间创建失败,它应该在失败的房间实体之前销毁所有创建的数据。

CenterController创建:

create: function(req, res) {

    console.log('params: ', req.params.all());
    var centerObj = {
        name: req.param('center_name'),
        state: req.param('center_state')
    };
    var roomsInput = req.params('rooms');

    console.log('created center centerObj: ', centerObj);
    Center.create(centerObj, function centerCreated(err, center) {

        if (err) {
            console.log(err);
            req.session.flash = {
                err: err
            }

            console.log("Error in center create")
            return res.redirect('/center/new');
        }

        // keep track of successfully created rooms
        var created_rooms_ids = new Array();
        async.mapSeries(

            // array to iterate over
            roomsInput,

            // iterator function
            function(roomInput, cb)
            {
                var roomObj = {
                    name: roomInput.name,
                    center: center.id,
                    min_age: roomInput.min_age,
                    max_age: roomInput.max_age
                };

                Room.create(roomObj, function roomCreated(err, room) {
                    if (err) {
                        console.log("Room.create error: ", err);
                        return cb(err);
                    }
                    created_rooms_ids.push(room.id);
                    return cb(null, room.id);
                });
            },

            // callback for when the loop is finished
            function(err, results)
            {
                if (err) {
                    console.log('error');
                    return destroyCreatedResources(err);
                }
                console.log('center: ', center);
                return res.redirect('/center/show/' + center.id);

                // destroy created resources (center + room)
                function destroyCreatedResources(err)
                {
                    console.log("destroyCreatedResources. Center=", center.id, "Id=", created_rooms_ids);
                    Center.destroy({id: center.id}).exec(function(e){
                        Room.destroy({id: created_rooms_ids}).exec(function(e){
                            console.log('Room.destroy');
                            if (e) {console.log('Room.destroy error!!!!!!!!!!!!!!!!!!!!!!');}
                            return res.serverError(err);
                        });
                    });
                }
            }
        );
    });
},

问题

当中间发生错误并且我想对所有创建的房间执行反向销毁时,仅中心被销毁。 怎么来res.serverError(错误);在房间被毁之前被召唤?

                function destroyCreatedResources(err)
                {
                    Center.destroy({id: center.id}).exec(function(e){
                            console.log('Room.destroy');
                            if (e) {console.log('Room.destroy error!!!!!!!!!!!!!!!!!!!!!!');}
                        Room.destroy({id: created_rooms_ids}).exec(function(e){
                            return res.serverError(err);
                        });
                    });
                }

是否有更好的方法进行反向销毁?

1 个答案:

答案 0 :(得分:0)

看起来这是由于sails-mongo v0.10.0-rc2中的错误造成的。现在已经patched并发布为v0.10.0-rc3,因此您可以从npm下拉最新版本并解决问题。谢谢!