I think you problem is here

        jsonReservations, err := 
jsonConvert.ReservationsJson(otherReservations)
        if err != nil {
                rw.WriteHeader(http.StatusExpectationFailed)
                fmt.Println(err)
        }

Instead try this

   if err != nil {
         http.Error(rw, err.Error(), http.StatusExpectationFailed)
         return // this is the important part
   }
   // do not allow execution to get to this line if err is != nil

On Thursday, 24 November 2016 11:44:13 UTC+11, gkol...@gmail.com wrote:
>
> Hey im running a go server locally using gorilla mux and net/http.
>
> For class we needed to create a room reservation system. I was testing the 
> application on multiple compyters on the same network but 
> when trying to create a reservation from both computer at the same time to 
> test concurrency I get a multiple write headers error. This problem does 
> not occur if i'm
> using the same computer but running on different browsers.
>
> It may help to know that the create button runs 2 ajax calls one to to 
> /readReservationByUserId as POST and to /createReservation as POST.
>
> func GetReservationsOthers(rw http.ResponseWriter, req *http.Request) {
>         abstractTdg := mappers.MapperBundle.UserMapper.UserTdg.AbstractTdg
>         abstractTdg.GetConnection()
>         defer abstractTdg.CloseConnection()
>         defer req.Body.Close()
>         req.ParseForm()
>
>         roomID, err := strconv.Atoi(req.FormValue("dataRoom"))
>         userID, err := strconv.Atoi(req.FormValue("userID"))
>         reservationsMapper := mappers.MapperBundle.ReservationMapper
>         reservations, err := reservationsMapper.GetByRoomId(roomID)
>
>         otherReservations := 
> reservationsMapper.FilterOutUser(reservations, userID)
>
>         if err != nil {
>                 rw.WriteHeader(http.StatusExpectationFailed)
>                 fmt.Println(err)
>         }
>
>         jsonReservations, err := 
> jsonConvert.ReservationsJson(otherReservations)
>         if err != nil {
>                 rw.WriteHeader(http.StatusExpectationFailed)
>                 fmt.Println(err)
>         }
>         rw.Header().Set("Content-Type", "application/json")
>         rw.Write(jsonReservations)
>
> }
>
> func CreateReservation(rw http.ResponseWriter, req *http.Request) {
>         abstractTdg := mappers.MapperBundle.UserMapper.UserTdg.AbstractTdg
>         abstractTdg.GetConnection()
>         defer abstractTdg.CloseConnection()
>         req.ParseForm()
>         roomId := req.FormValue("dataRoom")
>         userId := req.FormValue("userID")
>         startTime := req.FormValue("startTime")
>         endTime := req.FormValue("endTime")
>         roomIdint, _ := strconv.Atoi(roomId)
>         userIDint, _ := strconv.Atoi(userId)
>         startTimeformated, _ := time.Parse("2006-01-02 15:04:05", 
> startTime)
>         endTimeformated, _ := time.Parse("2006-01-02 15:04:05", endTime)
>         reservationMapper := mappers.MapperBundle.ReservationMapper
>
>         if err := reservationMapper.Create(roomIdint, userIDint, 
> startTimeformated, endTimeformated); err != nil {
>                 rw.WriteHeader(http.StatusExpectationFailed)
>         }
>
>         rw.WriteHeader(http.StatusOK)
>         bytes, _ := jsonConvert.MessageJson("Success")
>         rw.Write(bytes)
> }
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to