question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error with "getBuffer()"

See original GitHub issue

I tried to use the getBuffer function detail here https://github.com/form-data/form-data#buffer-getbuffer

to get my from as buffer for axios but i end up with an error. I tried to reproduce with the smallest possible code (the issue is when the getBuffer method is call):

const fs = require('fs');
// To add image to post
var FormData = require('form-data');

var formData = new FormData();
// Fill the formData object
formData.append('dum', 'dum');
// load picture image
formData.append('my_file', fs.createReadStream('/tmp/CatWorried1.jpg'));

var aBuff=formData.getBuffer();

I have the following error when calling this code

[root@cc1b78c1db72 myapp]# npm start

myapp@1.0.0 start /myapp node index.js

buffer.js:219 throw new ERR_INVALID_ARG_TYPE( ^

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type object at Function.from (buffer.js:219:9) at FormData.getBuffer (/myapp/node_modules/form-data/lib/form_data.js:328:57) at Object.<anonymous> (/myapp/index.js:12:20) at Module._compile (internal/modules/cjs/loader.js:777:30) at Object.Module._extensions…js (internal/modules/cjs/loader.js:788:10) at Module.load (internal/modules/cjs/loader.js:643:32) at Function.Module._load (internal/modules/cjs/loader.js:556:12) at Function.Module.runMain (internal/modules/cjs/loader.js:840:10) at internal/main/run_main_module.js:17:11 npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! myapp@1.0.0 start: node index.js npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the myapp@1.0.0 start script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in: npm ERR! /root/.npm/_logs/2019-08-18T20_32_47_733Z-debug.log

I tried to debug without success and had a look on the existing ticket and only found this one that is related to this function and type https://github.com/form-data/form-data/issues/427 but it seems already closed.

I wonder if i miss something or if there a real issue there. any advice to debug would be welcome.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
JBtjecommented, Aug 20, 2019

createReadStream returns type ReadStream which is an object. Objects cannot be added to buffer hence the error.

If you use readFileSync, it will return a Buffer and it should work. Question is, is it logical to support a stream when you want to obtains a buffer that you can “hold”?

Example with readFileSync: https://runkit.com/jbtje/5d5ba44f7c85ae001a1d201f

Note: does not take away that the example is wrong 😐

0reactions
tvk-codecraftcommented, Dec 12, 2019

@JBtje, yeah, I am able to understand what you mean. Receiving as application/octet-stream needs additional step to covert the type. Although it can be done, it adds complexity.

Is there anyway, to set the data in FormData along with the content type, as exactly same way it happens with web browsers?

I am using FormData in my test cases to make upload requests for tests. Currently, for now, I added a work around in the server code to check the NODE_ENV & set the content type manually.

Read more comments on GitHub >

github_iconTop Results From Across the Web

why is get error with getBuffer()? - java - Stack Overflow
The method getBuffer is undefined for java.io.Writer . Declare as StringWriter : StringWriter outWriter = new StringWriter();.
Read more >
GetBuffer() problem - MSDN - Microsoft
You have to change character set to 'Use Multibyte Character Set' in your project setting. But why you want to convert it from...
Read more >
Re: CString::GetBuffer [edited due to slight error] - C / C++ / MFC ...
Ok so is I'm using a CString object to store contents read from a text file, whose size is unknown yet, I'd better...
Read more >
io — Core tools for working with streams — Python 3.11.1 ...
For example giving a str object to the write() method of a binary stream will raise a ... io.open(file, mode='r', buffering=- 1, encoding=None,...
Read more >
ERROR: CMMALPool::GetBuffer - LibreELEC Forum
2021-02-09 17:01:11.726 T:1626039152 ERROR: CMMALPool::GetBuffer ... 2021-02-09 17:01:38.502 T:3011150432 NOTICE: CVideoPlayer::CloseFile()
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found