0

所以我试图在 ReactJS 组件中导入一个异步函数(即在一个单独的文件中)并最终出现以下错误:

TypeError: The "original" argument must be of type Function
promisify
node_modules/util/util.js:601
  598 | var kCustomPromisifiedSymbol = typeof Symbol !== 'undefined' ? Symbol('util.promisify.custom') : undefined;
  599 | 
  600 | exports.promisify = function promisify(original) {
> 601 |   if (typeof original !== 'function')
  602 |     throw new TypeError('The "original" argument must be of type Function');
  603 | 
  604 |   if (kCustomPromisifiedSymbol && original[kCustomPromisifiedSymbol]) {

我尝试导入的异步函数如下所示:(文件名:pa11y_script.js)

const pa11y = require("pa11y");
async function runPa11y(){
        try{
            const results = await pa11y('https://solferinoacademy.com/');
            return true;
        } catch (error) {
            return false;
        }
    }
    
export default runPa11y;

我试图在其中导入上述函数的组件如下所示:(文件名:Accessibility.js)

import React, { useState } from "react";
import style from "./Accessibility.module.css";
import Button from "react-bootstrap/button";
import runPa11y from "./pa11y_script";

const Accessibility = () => {

    const [url, updateURL] = useState("");
    const [assessmentResults, updateResultSection] = useState("");

    // Function for triggering the accessibility assessment.
    const perform_assessment = () => {
        runPa11y();
    }

    return (<>
        <div className={style["main-div"]}>
            <div className={style["navbar-div"]}>
                <div><h4>Accessibility Assessment</h4></div>
                <div className={style["control-panel"]}>
                    <div className={style["url-input"]}><input placeholder="Enter the URL ..." value={url} onChange={event => updateURL(event.target.value)} /></div>
                    <div className={style["button-section"]}>
                        <div><Button variant="dark" onClick={() => perform_assessment()}>Perform Accessibility Test</Button></div>
                        <div><Button variant="dark">View Previous Report</Button></div>
                    </div>
                </div>
            </div>
            <div className={style["results-section"]}>

            </div>
        </div>
    </>)
}

export default Accessibility;

我注意到,每当我从组件文件 ( ) 中删除import runPa11y from "./pa11y_script";和删除时,我都不会再收到此错误,这证实了导入此函数是导致错误的原因。我尝试了许多不同的方法来解决这个问题,但找不到解决方法。我什至尝试了这个stackoverlfow 帖子中建议的不同方法,但仍然面临同样的问题。如果有人能指导我如何解决这个问题,那就太好了。谢谢!runPa11y();Accessibility.js

4

0 回答 0