Skip to content
the poor coder

The "data" argument must be one of type string, Buffer, TypedArray, or DataView. Received type object

Poor Man
Published
Reading time
3 min read

The "data" argument must be one of type string, Buffer, TypedArray, or DataView. Received type object#

If you are working with Node.js and trying to send data to a server or trying to read data from a file, you might encounter an error message that says "The 'data' argument must be one of type string, Buffer, TypedArray, or DataView. Received type object". This error message usually occurs when you are trying to pass an object to a function that only accepts specific data types.

Let's say you are trying to send a JSON object to a server using the 'http' module in Node.js:

Text
const http = require('http');

const data = {
  name: 'John',
  age: 30
};

const options = {
  hostname: 'localhost',
  port: 3000,
  path: '/',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  }
};

const req = http.request(options, (res) => {
  console.log(`statusCode: ${res.statusCode}`);
  
  res.on('data', (d) => {
    process.stdout.write(d);
  });
});

req.on('error', (error) => {
  console.error(error);
});

req.write(data);
req.end();

When you run this code, you will get the following error message:

Text
TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be one of type string, Buffer, TypedArray, or DataView. Received type object
    at new NodeError (node:internal/errors:371:5)
    at write_ (node:_http_outgoing:722:11)
    at ClientRequest.write (node:_http_outgoing:687:15)
    at Object.<anonymous> (/path/to/file.js:22:5)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'ERR_INVALID_ARG_TYPE'
}

The error message is telling us that the 'data' argument passed to the 'req.write' function is of type object, which is not one of the accepted data types. To fix this error, we need to convert the JSON object to a string using the 'JSON.stringify' method:

Text
const http = require('http');

const data = {
  name: 'John',
  age: 30
};

const options = {
  hostname: 'localhost',
  port: 3000,
  path: '/',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  }
};

const req = http.request(options, (res) => {
  console.log(`statusCode: ${res.statusCode}`);
  
  res.on('data', (d) => {
    process.stdout.write(d);
  });
});

req.on('error', (error) => {
  console.error(error);
});

req.write(JSON.stringify(data));
req.end();

Now when you run the code, it should work without any errors.

Other ways to fix the error#

If you are working with a different module or function that requires a specific data type, you can try the following:

  • If you need to pass an object, convert it to a string using 'JSON.stringify'
  • If you need to pass an array, convert it to a string using 'JSON.stringify' or create a TypedArray from the array
  • If you need to pass a number or boolean, convert it to a string using 'toString' method
  • If you need to pass a date, convert it to a string using 'toISOString' method
  • If you need to pass a file, read the file using 'fs.readFile' and pass the buffer returned by this function

By understanding the data types accepted by different functions and modules, and by converting your data to the correct type, you can avoid errors like "The 'data' argument must be one of type string, Buffer, TypedArray, or DataView. Received type object".

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC