如何解释 node / javascript 错误消息

How to interpret node / javascript error message

我正在尝试使用 Node.js 在客户端的浏览器上显示一个数字,但是 Git bash 给我一个错误

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer. Received type number (2)

这里的问题是,一旦我将评估比例设置为 main2.js,控制台就会给出一个 TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer. Received type number (2)。问题可能出在 response.write(evaluator.getGrade(35));?

一旦在 javascript 模块中设置分数,该数字代表学生成绩。 20分1级,35分2级,以此类推

这是代码片段:

evaluate2.js

let _scale;
const setEvaluationScale = scale => {
  _scale = scale;
}
const simpleTest = () => { return 'Ei ollutkaan piilossa??'}
const getGrade = points => {
  let grade = 0;
  if(!_scale) {
    return 'There is no evaluation scale defined.';

  }
  for(let i = 0; i < _scale.length; i++){
    if (points >= _scale[i].points){
      grade = _scale[i].grade;
    }
  }

return grade;
}
module.exports.setEvaluationScale = setEvaluationScale;
module.exports.getGrade = getGrade;

这是浏览器的代码:

main2.js

const port = 3000,
http = require("http"),
httpStatus = require("http-status-codes"),
app = http.createServer((request, response) => {
    console.log("Received an incoming request!");
    response.writeHead(httpStatus.OK, {
        "Content-Type": "text/html"
    });
    const evaluator = require('./evaluate2.js');
    evaluator.setEvaluationScale([{ grade: 1, points: 20 }, { grade: 2, points: 35 }, { grade: 3, points: 50 }, { grade: 4, points: 65 }, { grade: 5, points: 80 }]);
    response.write(evaluator.getGrade(35));
    response.end();
    console.log(`Sent a response : ${evaluator.getGrade(20)}`);
});
app.listen(port);
console.log(`The server has started and is listening on port number: ${port}`);

main2.js的基本结构来自于:

 const port = 3000,
 http = require("http"),
 httpStatus = require("http-status-codes"),
 app = http.createServer((request, response) => {
 console.log("Received an incoming request!");
 response.writeHead(httpStatus.OK, {
"Content-Type": "text/html"
 });
 let responseMessage = "<h1>Hello, Universe!</h1>";
 response.write(responseMessage);
 response.end();
 console.log(`Sent a response : ${responseMessage}`);
 });
app.listen(port);
console.log(`The server has started and is listening on port number:
➥ ${port}`);

在 Git-Bash 中执行后,它会将 Hello Universe 打印到浏览器。一旦我尝试通过 require/exports 方法使用外部文件,它就会一直工作,直到应将特定数值提供给浏览器。

首先,git和git-bash都与这个错误无关。您只是使用 git-bash 作为 shell 来启动您的 nodejs 服务器代码。

其次,实际的错误消息,包括堆栈跟踪(error at whatever ... at whatever)是查找此错误的最重要的工具。没有堆栈跟踪,错误消息几乎没有意义

第三,堆栈跟踪的这一行标识了引发错误的代码。

at Server.<anonymous> (C:\Users\adm\Desktop\Server\Unit1\simple_server\main2.js:13:11) 

您可以在此处识别您的代码,因为您可能知道 main2.js 是您的。这表示您在第 13 行字符位置 11 处的代码导致了错误。 该行代码在一个匿名函数中(一个没有自己名称的函数)。通过计算 main2.js 中的行数,您可以确定有问题的行是

 response.write(responseMessage);

四、现在看第一行报错信息

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer. Received type number (2)

您的行调用 response.write() 时带有一个参数。错误消息告诉您参数必须是 string 或某种缓冲区,但您的参数是 number.

所以,现在,您知道您的值 responseMessage 需要先从数字更改为字符串,然后才能在 response.write() 中使用它。你怎么能改变它?有很多方法。这里有两个可能的选择:

  1. 使用response.write(responseMessage.toString())

  2. 使您的 getGrade() 函数 return 成为字符串而不是数字。

是的,这令人困惑。 Javascript 应该给你一种错觉,即字符串和数字彼此工作相同。但是你使用的是 http 包,它不会给你那种错觉。该包允许您 .write() 将信息发送到您的浏览器。您的浏览器需要文本字符串或包含要呈现的页面的缓冲区。

专业提示:使用显示行号的文本编辑器,这样您就不必计算它们了。