Reputation: 55
I'm trying to get a number visible on the browser in the client side using Node.js but Git bash is giving me an error
TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer. Received type number (2)
The issue here is that once I set the evaluationscale to my main2.js, console is giving a
TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer. Received type number (2)
. Could the problem be in response.write(evaluator.getGrade(35));?
The number represents a student grade once points are set in the javascript module. 20 points equals grade 1, 35 points equals grade 2 and so on.
Here is the code snippet:
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;
And here is the code for the browser:
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}`);
The fundamental structure of main2.js is from this:
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}`);
Once executed in Git-Bash it prints the Hello universe to the browser. Once I am trying to use external file with require/exports method, it works until specific numeric value should be given to the browser.
Upvotes: 0
Views: 243
Reputation: 108706
First of all, neither git nor git-bash has anything to do with this error. You're simply using git-bash as a shell to start your nodejs server code.
Second, the actual error message, including the stack trace ( error at whatever ... at whatever) is the single most important tool you have for finding this bug. Without the stack trace the error message is almost meaningless
Third, this line of the stack trace identifies your code which threw the error.
at Server.<anonymous> (C:\Users\adm\Desktop\Server\Unit1\simple_server\main2.js:13:11)
You can recognize your code here, because presumably you know main2.js
is yours. This says that your code at line 13, character position 11, caused the error.
That line of code is in an anonymous function (a function without its own name). By counting lines in main2.js
you can determine that the offending line is
response.write(responseMessage);
Fourth, now look at the first line of the error message
TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer. Received type number (2)
Your line calls response.write()
with a single argument. The error message tells you that argument must be a string
or some kind of buffer, but that your argument is a number
.
So, now, you know your value responseMessage
needs to be changed from a number to a string before you use it in response.write()
. How can you change it? There are many ways. Here are two possible choices:
Use response.write(responseMessage.toString())
Make your getGrade()
function return a string instead of a number.
Yes, this is confusing. Javascript is supposed to give you the illusion that strings and numbers work the same as each other. But you're using the http package, which doesn't give you that illusion. that package lets you .write()
information to send to your browser. Your browser expects either a text string or a buffer containing a page for it to render.
Pro tip: Use a text editor that shows line numbers, so you don't have to count them.
Upvotes: 1