I have a POST form in html with this structure:
name1 email1 address1
on a button press a new div with the exact same fields appears with a number higher
This data gets posted to an express function where a foreach loop is supposed to fetch the data.
The problem I am having is incrementing the req.body.name in the foreach
The following code is the closest I have gotten, however I always get a "NaN" error
app.post('/submit', function(req, res) {
for (var i = 1; i <= req.body.numberOfItems; i++) {
console.log(req.body.name += i);
}}
In your code you are incrementing the req.body.name
value.
You should change the key only
app.post('/submit', function(req, res) {
for (var i = 1; i <= req.body.numberOfItems; i++) {
console.log(req.body["name"+i]);
}}
Thank you for that, worked like a charm. As I am not using an array I thought I could not change the key.
@Julius this is not an array. As you are using dynamic object keys (name1, name2, name3), you should use brackets instead of dot to specify the key on the
req.body
object