Warm tip: This article is reproduced from serverfault.com, please click

Concat to request before requesting

发布于 2020-11-28 10:24:35

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);
}}
Questioner
Julius
Viewed
0
Ozgur Sar 2020-11-28 18:38:52

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]);
}}