سؤال

i'm developing an app with sails.js beta and mongodb. I've two models in a many-to-many association, i can successfully associate and populate instances of these models using .add() and .populate() methods. My problem is now that the .remove() method seems to do nothing.

here the models:

//Menu.js
module.exports = {
  schema : true,
  attributes: {
    name: {
        type: 'string',
        minLength: 3,
        required: true
    },
    dishes: {
        collection: 'dish',
        via: 'menus',
        dominant: true
    }
  }

};

//Dish.js
module.exports = {
schema : true,
attributes: {
    name:  {
        type: 'string',
        minLength: 3,
        required: true
    },
    description: 'string',
    menus: {
        collection: 'menu',
        via: 'dishes'
    }
  }

};

And here the controller actions...

addDishToMenu: function(req,res,next){
    Menu.findOne(req.param('menu')).populate('dishes').exec(function(err,bean){
        if(err) return next(err);
        if(!bean) return next();
        bean.dishes.add(req.param('dish'));            
        bean.save(function(err) {
            if(err) return next(err);
            res.redirect('/main/dishes/');
        })
    })
},

removeDishFromMenu: function(req,res,next){
    Menu.findOne(req.param('menu')).populate('dishes').exec(function(err,bean){
        if(err) return next(err);
        if(!bean) return next();
        bean.dishes.remove(req.param('dish'));
        bean.save(function(err) {
            if(err) return next(err);                
            res.redirect('/main/menu/' + req.param('menu'));
        })
    })
}

I can't figure out what i'm doing wrong. Any ideas?

هل كانت مفيدة؟

المحلول

This issue has been fixed and I confirmed it with the repo I sent earlier. If you update your sails, waterline, and sails-mongo versions you should be good to go.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top