node warnings: UnhandledPromiseRejectionWarning: TypeError: displayErrorFunction is not a function

node warnings: UnhandledPromiseRejectionWarning: TypeError: displayErrorFunction is not a function

我们有两种代码变体,它们都return相同的节点警告。

variant 1

import axios from 'axios';

const correctEndpoint = `https://${process.env.AUTH0_DOMAIN}/dbconnections/signup`;
const headers = {
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    Accept: '*/*'
  }
};

const registerWithAuth0 = async (payload, redirectFunction, displayErrorFunction) => {
  try {
    const response = await axios.post(correctEndpoint, payload, headers);
    if (response.status === 200) {
      redirectFunction();
    } else {
      displayErrorFunction();
    }
  } catch (err) {
    displayErrorFunction();
  } 
}

export default registerWithAuth0;

variant 2

import axios from 'axios';

const correctEndpoint = `https://${process.env.AUTH0_DOMAIN}/dbconnections/signup`;
const headers = {
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    Accept: '*/*'
  }
};

const registerWithAuth0 = (payload, redirectFunction, displayErrorFunction) => {
  axios.post(correctEndpoint, payload, headers)
    .then((response) => {
      if (response.status === 200) {
        redirectFunction();
      } else {
        displayErrorFunction();
      }
    })
    .catch (() => {
      displayErrorFunction();
    });
}

export default registerWithAuth0;

所有笑话测试都通过了,但我们可以看到以下一些节点警告

(node:26886) UnhandledPromiseRejectionWarning: TypeError: displayErrorFunction is not a function
(Use `node --trace-warnings ...` to show where the warning was created)
(node:26886) 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: 1)
(node:26886) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:26886) UnhandledPromiseRejectionWarning: TypeError: displayErrorFunction is not a function
(node:26886) 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: 3)

我们正在使用:

关于这里可能出现的问题有什么想法吗?

在线报告了一些类似的问题,但不完全相同:

确保任何测试用例都缺少该参数。

或者您可以为 displayErrorFunction 添加默认值。

(payload, redirectFunction, displayErrorFunction = ()=>{} ) => {

}