如何解释节点/ javascript错误消息

问题描述 投票:0回答:1

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

TypeError [ERR_INVALID_ARG_TYPE]:第一个参数必须是类型字符串或Buffer的实例。收到的类型编号(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方法一起使用,它将起作用,直到应为浏览器指定特定的数值为止。

node.js module typeerror stack-trace
1个回答
0
投票

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

第二,实际的错误消息,包括堆栈跟踪(发生错误……发生任何错误)是查找此错误的最重要的工具。没有堆栈跟踪,错误消息几乎没有意义

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

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

您可以在这里识别您的代码,因为大概您知道main2.js是您的代码。这表示您的代码在第13行,字符位置11,引起了错误。那条线是

 response.write(responseMessage);

第四,现在看错误消息的第一行

TypeError [ERR_INVALID_ARG_TYPE]:第一个参数必须为字符串类型或Buffer的实例。收到的类型编号(2)

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

因此,现在,您知道在responseMessage中使用值response.write()之前需要将其从数字更改为字符串。您该如何更改?有很多方法。这是两个:

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

  2. 使您的getGrade()函数返回字符串而不是数字。

是的,这很令人困惑。 Javascript应该给您一种幻觉,即字符串和数字彼此相同。但是您使用的是http程序包,不会给您带来错觉。

© www.soinside.com 2019 - 2024. All rights reserved.