I need assistance with sending an array of objects to a post route in my express app.
Here is my form (written in ejs format):
<form action="/archiveList/<%= list._id %>" method="POST">
<input type="hidden" name="list" value = <%= items %> >
</form>
The code for my post route is:
router.post("/archiveList/:id", function (req,res){
var array = req.body.list;
array.forEach(function(obj){
console.log(obj.name)
res.redirect("/main");
});
When I implement this, I encounter the following error: "array.forEach is not a function" If I try:
console.log(array) // I get "[object"
Alternatively, if I check the data type using:
console.log(typeof(array)) // I get "string".
In my app.js file, I have included:
app.use(bodyParser.urlencoded({extended:true}));.
If I simply send a single string to the route and extract it using req.body, everything works fine. There appears to be something different about sending an array of objects and extracting it with body parser that I can't quite figure out. Any assistance would be greatly appreciated.