Tuesday 30 May 2017

Checkbox data with MongoDB

The following code allows user's to check a box to include an amenity on a post using MongodB/Node/Express. It is working perfectly to let users select their amenities and then shows those selections as icons.

However I am unable to EDIT these posts in my edit route . That is the problem. See code at bottom. How would I change my code to be able to edit the icons?

// Simplified Schema

   var rentalsSchema = new mongoose.Schema({
       amenities: Array
    });

// User Checks their amenities

<input type="checkbox" name="amenities" value="wifi"><span>Wi-fi</span>
<input type="checkbox" name="amenities" value="tv"><span>TV</span>

// This code loops through the array and shows the relevant icons using Google's material icons.

  <div class="list-group">
       <li class="list-group-item active">Amenities</li>
       <li id="amenitiesBox" class="list-group-item">
        <% for(var i=0; i<rentals.amenities.length; i++){ %>
        <i class="material-icons"><%=rentals.amenities[i]%></i>
        <% } %>
      </li>
      </li>
      </div>
      </div>

// The problem is here in my edit route. Simplified below

        <form id='rentalEdit' action="/rentals/<%=rentals._id %>/?_method=PUT" method ="POST"> 

 <input Checked type="checkbox" name="rentals[amenities]" value="wifi"><span>Wifi</span> 

        </form>



via AndrewLeonardi

No comments:

Post a Comment