# Fluent querying problem

**URL:** <https://forums.kodeco.com/t/fluent-querying-problem/62132>\
**Category:** Server Side Swift with Vapor\
**Created:** [February 13, 2019, 2:07pm UTC](https://forums.kodeco.com/t/fluent-querying-problem/62132 "2019-02-13T14:07:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mfilipowicz](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/mfilipowicz/32/102075_2.png) [@mfilipowicz](https://forums.kodeco.com/u/mfilipowicz)\
**Post date:** [February 13, 2019, 2:07pm UTC](https://forums.kodeco.com/t/fluent-querying-problem/62132/1 "2019-02-13T14:07:07Z")

</div>

I’m making a simple ride-sharing app and have the following scenario:

When a user makes a request to /api/trip/new to create a new trip, I want to retrieve the first driver from the database and assign their ID to a Trip object. Then I want to save both the trip and trip request in the database.

I have the following code:

```
func newTripHandler(_ req: Request, data: TripRequestCreateData) throws -> Future<TripRequest> {
let passenger = try req.requireAuthenticated(Passenger.self)
let tripRequest = try TripRequest(pickupLat: data.pickupLat,
                                  pickupLng: data.pickupLng,
                                  destLat: data.destLat,
                                  destLng: data.destLng,
                                  pickupAddress: data.pickupAddress,
                                  destAddress: data.destAddress,
                                  passengerId: passenger.requireID())

//Passenger's location
let lat = tripRequest.pickupLat
let lng = tripRequest.pickupLng

//Passenger's destination
let toLat = tripRequest.destLat
let toLng = tripRequest.destLng

let trip = Trip(passengerId: tripRequest.passengerId!,
                pickupLat: lat,
                pickupLng: lng,
                destLat: toLat,
                destLng: toLng,
                pickupAddress: tripRequest.pickupAddress,
                destAddress: tripRequest.destAddress)

let driver = Driver.query(on: req).first().unwrap(or: Abort(.notFound))

let driverId = driver.map(to: Driver.self) { driver in
  return driver.id
}

trip.driverId = driverId

print(trip)

return tripRequest.save(on: req)

```

}

```
struct TripRequestCreateData: Content {
  let pickupLat: Double
  let pickupLng: Double
  let destLat: Double
  let destLng: Double
  let pickupAddress: String
  let destAddress: String
}

```

I’m not sure how to save two models and how to properly retrieve the driver from the database. The driverId constant is of type EventLoopFuture instead of Driver.

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [February 13, 2019, 3:50pm UTC](https://forums.kodeco.com/t/fluent-querying-problem/62132/2 "2019-02-13T15:50:05Z")

</div>

@0xtim Can you please help with this when you get a chance? Thank you - much appreciated! :]

---

<div class="post-metadata">

**Author:** ![mfilipowicz](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/mfilipowicz/32/102075_2.png) [@mfilipowicz](https://forums.kodeco.com/u/mfilipowicz)\
**Post date:** [February 13, 2019, 5:43pm UTC](https://forums.kodeco.com/t/fluent-querying-problem/62132/3 "2019-02-13T17:43:42Z")

</div>

Figured it out

```
return Driver.query(on: req).first().unwrap(or: Abort(.notFound)).flatMap { driver in
  trip.driverId = driver.id
  
  return trip.save(on: req).flatMap { trip in
    return tripRequest.save(on: req)
  }
}

```

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [February 13, 2019, 6:36pm UTC](https://forums.kodeco.com/t/fluent-querying-problem/62132/4 "2019-02-13T18:36:25Z")

</div>

@mfilipowicz Glad you fixed it! Cheers! :]
