0

I have a HTML and JS code written and is fetching data from the link '/availability-json'

<div class="col-lg-4">
                    <div class="room-booking">
                        <h3>Your Reservation</h3>
                        <form id="f2" method="post" action="" class="needs-validation">
                            <input type="hidden" name="room_id" value="1">
                            <div class="check-date">
                                <label for="date-in">Check In:</label>
                                <input type="text" class="date-input" name="start" id="date-in" autocomplete="off">
                                <i class="icon_calendar"></i>
                            </div>
                            <div class="check-date">
                                <label for="date-out">Check Out:</label>
                                <input type="text" class="date-input" id="date-out" name="end" autocomplete="off">
                                <i class="icon_calendar"></i>
                            </div>
                            <div class="select-option">
                                <label for="guest">Guests:</label>
                                <select id="guest">
                                    <option value="">3 Adults</option>
                                </select>
                            </div>
                            <div class="select-option">
                                <label for="room">Room:</label>
                                <select id="room">
                                    <option value="">1 Room</option>
                                </select>
                            </div>
                            <button type="submit" id="b2">Check Availability</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </section>
    <!-- Room Details Section End -->

    <script>
        const dateIn = document.getElementById('date-in');
        const dateOut = document.getElementById('date-out');
        
        if (dateIn !== null && dateOut !== null) {
            document.getElementById('b2').onclick = function(event){
                event.preventDefault();
                console.log('clicked');

                let form = document.getElementById('f2');
                let formData = new FormData(form);
                formData.append("room_id", "1")

                fetch('/sona-master/availability-json', {
                    method: 'post',
                    body: formData,
                })
                    .then(response => response.json())
                    .then(data => {

                    if (data.ok) {
                        Notiflix.Notify.success('<p>Room is available</p>' + '<p><a href="#!" class="btn btn-primary">' + 'Book Now</a></p>')
                    } else {
                        Notiflix.Notify.failure('Room is not available')
                    })
               
            };
        }
    </script>

But my backend function for the link '/availability-json' is showing empty

import prologue, json
import times
import asyncdispatch
import strutils

import ../views/[index, layout, choose_room]
import ../database/roomrestrictions
import ../models/models

proc checkRoom*(ctx: Context) {.async.} =
  
  var sd = ctx.getFormParams("start")
  var
    ed = ctx.getPostParams("end")
  var

    startDate = parse(sd, "d MMMM, yyyy")
  var
    endDate = parse(ed, "d MMMM, yyyy")
    roomi = ctx.getPostParams("room_id")
  var

    roomID = parseInt(roomi)
    # roomID = parseInt(room_id)
  echo roomID
  var

    db = newDatabase3()

    available = db.searchAvailabilityByDatesByRoomID(startDate, endDate, roomID)

    a = %*
      {"ok": available, "message": "", "roomID": $roomID, "startDate": sd, "endDate": ed}

  resp jsonResponse(a)

Please help solve this problem and you can check the code in here

Robert
  • 7,394
  • 40
  • 45
  • 64
me0045
  • 1
  • 1

0 Answers0