UnhandledPromiseRejectionWarning:错误[ERR_HTTP_HEADERS_SENT]

时间:2020-10-04 19:51:25

标签: node.js

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
from matplotlib.colors import ListedColormap, BoundaryNorm

df = pd.read_excel("C:/Users/AmeliaB/Documents/Python/8-5in_plotting.xlsx", header=0)
df['DATETIME'] = pd.to_datetime(df.DATETIME)
# define variables
y = np.array(df["DHT001_Accel_Axial_Z_Max"])
x = np.array(df["Hole_Depth"])

# Create a set of line segments so we can color them individually
# This creates the points as a N x 1 x 2 array so that we can stack points
# together easily to get the segments. The segments array for line collection
# needs to be (numlines) x (points per line) x 2 (for x and y)
points = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)

fig, ax = plt.subplots(1)

cmap = ListedColormap(['green', 'orange', 'red', "maroon"])
norm = BoundaryNorm([1, 1.5, 2.5, 5, 5.5], cmap.N)
lc = LineCollection(segments, cmap=cmap, norm=norm)
lc.set_array(y)
lc.set_linewidth(1)  # The thickness of the line
ax.add_collection(lc)
ax.autoscale_view()
fig.colorbar(lc) #Add colorbar
plt.xlabel ("Hole depth")
plt.ylabel ("Vibration level")
plt.show()
UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
[0]     at ServerResponse.setHeader (_http_outgoing.js:526:11)
[0]     at ServerResponse.header (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:771:10)
[0]     at ServerResponse.send (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:170:12)
[0]     at ServerResponse.json (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:267:15)
[0]     at C:\Users\12345\Desktop\Capstone Project\src\controllers\/subjectController.js:35:43
[0]     at processTicksAndRejections (internal/process/task_queues.js:97:5)
[0] (node:7684) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 11)
[0] (node:7684) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
[0]     at ServerResponse.setHeader (_http_outgoing.js:526:11)
[0]     at ServerResponse.header (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:771:10)
[0]     at ServerResponse.send (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:170:12)
[0]     at ServerResponse.json (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:267:15)
[0]     at C:\Users\12345\Desktop\Capstone Project\src\controllers\/subjectController.js:35:43
[0]     at processTicksAndRejections (internal/process/task_queues.js:97:5)
[0] (node:7684) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 12)
[0] (node:7684) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
[0]     at ServerResponse.setHeader (_http_outgoing.js:526:11)
[0]     at ServerResponse.header (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:771:10)
[0]     at ServerResponse.send (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:170:12)
[0]     at ServerResponse.json (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:267:15)
[0]     at C:\Users\12345\Desktop\Capstone Project\src\controllers\/subjectController.js:35:43
[0]     at processTicksAndRejections (internal/process/task_queues.js:97:5)
[0] (node:7684) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 13)
[0] (node:7684) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
[0]     at ServerResponse.setHeader (_http_outgoing.js:526:11)
[0]     at ServerResponse.header (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:771:10)
[0]     at ServerResponse.send (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:170:12)
[0]     at ServerResponse.json (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:267:15)
[0]     at C:\Users\12345\Desktop\Capstone Project\src\controllers\/subjectController.js:35:43
[0]     at processTicksAndRejections (internal/process/task_queues.js:97:5)
[0] (node:7684) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 14)
[0] (node:7684) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
[0]     at ServerResponse.setHeader (_http_outgoing.js:526:11)
[0]     at ServerResponse.header (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:771:10)
[0]     at ServerResponse.send (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:170:12)
[0]     at ServerResponse.json (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:267:15)
[0]     at C:\Users\12345\Desktop\Capstone Project\src\controllers\/subjectController.js:35:43
[0]     at processTicksAndRejections (internal/process/task_queues.js:97:5)
[0] (node:7684) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 15)
[0] (node:7684) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
[0]     at ServerResponse.setHeader (_http_outgoing.js:526:11)
[0]     at ServerResponse.header (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:771:10)
[0]     at ServerResponse.send (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:170:12)
[0]     at ServerResponse.json (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:267:15)
[0]     at C:\Users\12345\Desktop\Capstone Project\src\controllers\/subjectController.js:35:43
[0]     at processTicksAndRejections (internal/process/task_queues.js:97:5)
[0] (node:7684) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 16)
[0] (node:7684) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
[0]     at ServerResponse.setHeader (_http_outgoing.js:526:11)
[0]     at ServerResponse.header (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:771:10)
[0]     at ServerResponse.send (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:170:12)
[0]     at ServerResponse.json (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:267:15)
[0]     at C:\Users\12345\Desktop\Capstone Project\src\controllers\/subjectController.js:35:43
[0]     at processTicksAndRejections (internal/process/task_queues.js:97:5)
[0] (node:7684) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled
[0]     at C:\Users\12345\Desktop\Capstone Project\src\controllers\/subjectController.js:35:43
[0]     at processTicksAndRejections (internal/process/task_queues.js:97:5)
[0] (node:7684) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 19)

1 个答案:

答案 0 :(得分:0)

至少可以将问题详细说明一下,而不是复制并粘贴错误消息。

据我所知,express出现错误,因为您试图多次返回响应。这就是为什么要进行for循环并在每次迭代中发送响应的原因。

批处理更新的正确方法是为每个用户检查db并更新或创建,每个操作的结果应存储在数组中。遍历所有来自请求的用户后,然后在响应上返回结果数组。

在无需过多更改代码的情况下,它可能看起来像:

router.post('/', passport.authenticate('jwt', { session: false }), (req, res) => {
    const result = []
    for (var i = 0; i < req.body.subjects.length; i++) {
        const subject = req.body.subjects[i];
        const { errors, isValid } = validateSubjectInput(subject);
        // Check validation
        if (!isValid) {
            result.push(errors);
        } else {
            const subjectFields = {};
            for (let key in subject) {
                if (subject[key]) subjectFields[key] = subject[key].trim();
            }
            subjectFields.id = subject.id ? subject.id.trim() : '';
            console.log(subjectFields);
            // See if there is already a subject with the subject ID
            // with option {new: true} it will create an user if not exists
            Subject.findOneAndUpdate(
                { _id: subject._id },
                { $set: subjectFields },
                { new: true } 
            ).then(subject => result.push(subject));
        }
    }
    res.json(result);
});