You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js中readline.question报TypeError: callback is not a function求助

解决readline.question()中TypeError: callback is not a function的问题

嘿,我一眼就看出问题所在啦!你在rl.question()的回调函数里额外加了一个callback参数,但**readline.question()的回调本身只接收一个参数——用户输入的文本内容**,根本不存在第二个callback参数,所以你代码里的callback其实是undefined,调用它的时候自然就抛出TypeError: callback is not a function错误了。

错误原因拆解

看你这段代码:

rl.question("./dict ", function(inputDictCmd, callback) { 
  // ... 中间逻辑
  request.get(requestUrl, function (err, res) { 
    callback("SJDKFD"); 
    return callback; 
  }); 
  // ... 其他逻辑
  rl.close(); 
});

这里的function(inputDictCmd, callback)是错误的,readline.question的回调签名是(answer: string) => void,只有inputDictCmd(用户输入内容)是有效参数,callback是你额外添加的,所以它的值是undefined,调用时必然触发错误。另外还有个小问题:你把rl.close()放在了rl.question回调的末尾,但request.get是异步操作,会导致readline还没等请求完成就被关闭。

修正方案

如果你想把请求结果传递出去,这里有两种实用的处理方式:

方式1:提前定义回调函数

先在外部写好处理结果的回调,然后在异步请求完成后调用它:

// 提前定义处理结果的回调
function handleResult(result) {
  console.log("获取到结果:", result);
  // 这里可以添加后续业务逻辑
}

rl.question("./dict ", function(inputDictCmd) { 
  if(inputDictCmd){ 
    const userInput = inputDictCmd.split(" "); 
    if(userInput.length === 2){ 
      const opr = userInput[0]; 
      const word = userInput[1]; 
      const requestUrl = "www.xyz.com"; 
      // 注意给逻辑判断加括号,避免优先级问题
      if(opr && (opr === 'defn' || opr === 'syn' || opr === 'ant' || opr === 'ex')){ 
        if(opr === 'defn'){ 
          const requestType = 'definitions'; 
          request.get(requestUrl, function (err, res) { 
            if (err) {
              console.error("请求出错:", err);
              rl.close();
              return;
            }
            handleResult("SJDKFD"); // 调用提前定义的回调
            rl.close(); // 请求完成后再关闭readline
          }); 
        } 
      } 
    } 
  } else {
    rl.close();
  }
});

方式2:用Promise封装(更现代的写法)

如果你的Node.js版本支持,可以用Promise封装readline和request的异步操作,代码会更清晰,还能配合async/await使用:

const { promisify } = require('util');

// 封装readline.question为Promise
const questionAsync = promisify(rl.question.bind(rl));
// 封装request.get为Promise
const getAsync = promisify(request.get);

async function processUserInput() {
  try {
    const inputDictCmd = await questionAsync("./dict ");
    if(inputDictCmd){ 
      const userInput = inputDictCmd.split(" "); 
      if(userInput.length === 2){ 
        const opr = userInput[0]; 
        const word = userInput[1]; 
        const requestUrl = "www.xyz.com"; 
        if(opr && (opr === 'defn' || opr === 'syn' || opr === 'ant' || opr === 'ex')){ 
          if(opr === 'defn'){ 
            const requestType = 'definitions'; 
            const res = await getAsync(requestUrl);
            console.log("结果:", "SJDKFD");
            // 这里处理请求结果
          } 
        } 
      } 
    }
  } catch (err) {
    console.error("处理出错:", err);
  } finally {
    rl.close(); // 无论成功失败都关闭readline
  }
}

processUserInput();

这样修改后,既解决了callback不存在的问题,也优化了异步操作的流程,避免了回调嵌套的混乱。

内容的提问来源于stack exchange,提问作者Abhishek Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 20:23:11