SHARE
TWEET

Untitled

a guest Jul 12th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. User{id, name, phone, email}
  2. Cars{id, name, manufacturer}
  3. UserCars{id, car_id, user_id, role}
  4.  
  5. User have many cars(through UserCars)
  6. Cars have many users(through UserCars)
  7.      
  8. router.get('/', async (req, res) => {
  9.  
  10.         try {
  11.  
  12.             let car = await Car.findOne({
  13.                 where: {
  14.                 id: req.car_id
  15.             }});
  16.  
  17.             let users = await car.getUsers({joinTableAttributes: ['role']})
  18.             res.send(users)
  19.  
  20.         } catch (e) {
  21.             console.log(e)
  22.             res.status(400).send(e)
  23.         }
  24.  
  25.     })
  26.      
  27. [
  28.     {
  29.         "id": 1,
  30.         "name": "test",
  31.         "email": null,
  32.         "phone": null,
  33.         "createdAt": "2019-07-09T09:38:11.859Z",
  34.         "updatedAt": "2019-07-12T04:34:20.922Z",
  35.         "User_car": {
  36.             "role": "driver"
  37.         }
  38.     }
  39. ]
  40.      
  41. [
  42.     {
  43.         "id": 1,
  44.         "name": "test",
  45.         "email": null,
  46.         "phone": null,
  47.         "role": 'driver'
  48.         "createdAt": "2019-07-09T09:38:11.859Z",
  49.         "updatedAt": "2019-07-12T04:34:20.922Z"
  50.     }
  51. ]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top