Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
456 views
in Technique[技术] by (71.8m points)

mysql - 如何在Node / Express中发送数组的每个迭代的Post请求?(How to send Post request for each iteration of an array in Node/Express?)

I have some raw json that I'm trying to send to my back end server in mysql.

(我有一些原始的json,我正尝试发送到mysql的后端服务器。)

I'm currently trying to loop through the specific array in the json that I need and sending data from each of the children in the array via a POST request but I am getting "Cannot set headers after they are sent to the client".

(我目前正在尝试遍历所需的json中的特定数组,并通过POST请求从数组中的每个子级发送数据,但是我收到“在将标头发送到客户端后无法设置标头”。)

app.post('/reddit-import', function (req, res) {
     console.log("Route /reddit-import POST");
     let data = req.body.data.children

     data.forEach(child => {

         let sql1 = `CALL insert_user('${child.data.author}', 
              '${child.data.author_fullname}');`
         connection.query(sql1, 
            data, 
            function (errQuery, result) {
               if (errQuery) {
                  console.log(errQuery);
                  res.json({status: "Error", err: errQuery});
                  res.end();
               } else {
                  console.log("Insert ID: ", result.insertId);
                  res.json({status: result.insertId, err: ""});
                  res.end();
               }
            }
         );

When I send the POST request, my backend gets 2 rows of data before it hits me with the error message...any ideas?

(当我发送POST请求时,我的后端会收到2行数据,然后它会出现错误消息,这使我震惊……有什么想法吗?)

  ask by johntc121 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You seem to be ending your outer response in the data.forEach with a res.end(), which I'm assuming is used to indicate the end of the outer HTTP request to the client.

(您似乎正在以res.end()结束data.forEach中的外部响应,我假设该响应用于向客户端指示外部HTTP请求的结束。)

Did you perhaps mean to use “result” there instead?

(您也许是想在此处使用“结果”吗?)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...