Check associations

You can also check if an object is already associated with another one (N:M only). Here is how you'd do it:

  1. // check if an object is one of associated ones:
  2. Project.create({ /* */ }).then(project => {
  3. return User.create({ /* */ }).then(user => {
  4. return project.hasUser(user).then(result => {
  5. // result would be false
  6. return project.addUser(user).then(() => {
  7. return project.hasUser(user).then(result => {
  8. // result would be true
  9. })
  10. })
  11. })
  12. })
  13. })
  14. // check if all associated objects are as expected:
  15. // let's assume we have already a project and two users
  16. project.setUsers([user1, user2]).then(() => {
  17. return project.hasUsers([user1]);
  18. }).then(result => {
  19. // result would be true
  20. return project.hasUsers([user1, user2]);
  21. }).then(result => {
  22. // result would be true
  23. })