I'm trying to find the very simplest way to use arrays within MongoDB and EJS.
In this very simple example on the front end a user can click a button and add a value to an array.
$("#buttonOne").click(function() {
food.push("Kiwi");
$("#foodObject").val(food)
});
$("#buttonTwo").click(function() {
food.push("Taco");
$("#foodObject").val(food)
});
Then on the front end see it within an input:
<input id='foodObject' type="text" name="foodObject" placeholder="foodObject">
The idea is then to pass the array to mongo
var UserSchema = new mongoose.Schema({
username: String,
password: String,
foodObject: [ ],
});
And then be able to use the array with EJS:
<h3 class='text-center'> <%= currentUser.foodObject[0] %> </h3>
So if a user had added both Kiwi and Taco to the array I'd want foodObject[0] to return just Kiwi.
Is this possible?
Currently it returns the full string. i.e "Kiwi, Taco"
<%= currentUser.foodObject[0] %>return all the array elements?