首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何解释节点/ javascript错误消息

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

Stack Overflow用户
提问于 2020-03-01 10:59:20
回答 1查看 189关注 0票数 0

我试图使用Node.js在客户端的浏览器上看到一个数字,但是Git给了我一个错误

TypeError ERR_INVALID_ARG_TYPE:第一个参数必须是string类型或缓冲区实例。接收类型编号(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分等于一级,35分等于二级,以此类推。

下面是代码片段:

求值2.js

代码语言:javascript
复制
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

代码语言:javascript
复制
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的基本结构是这样的:

代码语言:javascript
复制
 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宇宙。一旦我试图使用外部文件与请求/导出方法,它可以工作,直到特定的数值应该给浏览器。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-01 12:47:22

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

第二,实际的错误消息,包括堆栈跟踪(任何.在任何情况下)都是您找到这个bug的最重要的工具。如果没有堆栈跟踪,错误消息几乎是毫无意义的。

第三,堆栈跟踪的这一行标识抛出错误的代码。

代码语言:javascript
复制
at Server.<anonymous> (C:\Users\adm\Desktop\Server\Unit1\simple_server\main2.js:13:11) 

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

代码语言:javascript
复制
 response.write(responseMessage);

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

TypeError ERR_INVALID_ARG_TYPE:第一个参数必须是string类型或缓冲区实例。接收类型编号(2)

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

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

  1. 使用response.write(responseMessage.toString())
  2. 使getGrade()函数返回字符串而不是数字。

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

专业提示:使用显示行号的文本编辑器,因此您不必对它们进行计数。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60474353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档