Bookshelf.js - how to save many-to-many relationship?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 01:51:40

What you want to do here is attach the ids to your relation, like this:

app.post('/users/', function(req, 
  console.log(req.body);
  var courses_ids = req.body.courses_ids; //array of ids
  delete req.body.courses_ids;
  new User(req.body).save()
    .then(function(user){
     // this is important
      return user.courses().attach(courses_ids);
    }).catch(function(error){
       console.log(error);
    });
});

It is also stated in the official docs: http://bookshelfjs.org/#Model-attach

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!