The "chunk" argument must be of type string or an instance of Buffer
Asked Answered
A

4

44

I'm running the following code and it fails with the below error.

AWS Code to list all objects inside a bucket.

const http = require('http');
const host = '127.0.0.1';
const port = 5000;
const path = require('path');
const url = require('url');
const fs = require('fs');
var AWS = require('aws-sdk');



const laptopDate = JSON.parse(fs.readFileSync(`${__dirname}/data/data.json`, `utf-8`));

AWS.config.update({accessKeyId: '***', secretAccessKey: '***', region: 'ap-south-1'});
s3 = new AWS.S3({apiVersion: '2006-03-01'});

var params = { 
    Bucket: 'bucket-name'
};

const server = http.createServer(function(req, res){
    const path = url.parse(req.url, true).pathname;
    const id = url.parse(req.url, true).query.id;

    if (path === 'bucket' || path === '/')
      s3.listObjects(params, function (err, data) {
        if(err) throw err;
        res.writeHead(200, { 'Content-Type': 'text/html' });
        //const output = JSON.parse(data)
        console.log(data);
        res.end(data);
      });
});

server.listen(port, host, function(req, res) {
    console.log(`Server is listening on ${host} and ${port}`)
});

The first output which is console.log displays everything as expected. However the res.end to render the output to the screen fails with the below error.

The "chunk" argument must be of type string or an instance of Buffer. Received an instance of Object
    at ServerResponse.end (_http_outgoing.js:752:13)
    at Response.<anonymous> (D:\js\Extra\starter\index.js:30:13)
    at Request.<anonymous> (D:\js\Extra\starter\node_modules\aws-sdk\lib\request.js:364:18)
    at Request.callListeners (D:\js\Extra\starter\node_modules\aws-sdk\lib\sequential_executor.js:106:20)
    at Request.emit (D:\js\Extra\starter\node_modules\aws-sdk\lib\sequential_executor.js:78:10)
    at Request.emit (D:\js\Extra\starter\node_modules\aws-sdk\lib\request.js:683:14)
    at Request.transition (D:\js\Extra\starter\node_modules\aws-sdk\lib\request.js:22:10)
    at AcceptorStateMachine.runTo (D:\js\Extra\starter\node_modules\aws-sdk\lib\state_machine.js:14:12)
    at D:\js\Extra\starter\node_modules\aws-sdk\lib\state_machine.js:26:10
    at Request.<anonymous> (D:\js\Extra\starter\node_modules\aws-sdk\lib\request.js:38:9) {
  message: 'The "chunk" argument must be of type string or an instance of Buffer. Received an instance of Object',
  code: 'ERR_INVALID_ARG_TYPE',
  time: 2020-05-18T08:39:24.916Z
}
Athiste answered 18/5, 2020 at 8:47 Comment(4)
If the response from S3 is an object, then just try -> res.json(data)Scream
I think it is probably good idea to remove AWS config params from this question :)Consecration
it fails even if i use res.json ************* D:\js\Extra\starter\node_modules\aws-sdk\lib\request.js:31 throw err; ^ Error [TypeError]: res.json is not a function at Response.<anonymous> (D:\js\Extra\starter\index.js:30:13) at Request.<anonymous> (D:\js\Extra\starter\node_modules\aws-sdk\libAthiste
@LazarNikolic, That was not the actual value of the AWS Keys ;-). Else by now i would have had multiple M# Large VM's running on my AWS Account:-)Athiste
S
42

Remove this res.writeHead(200, { 'Content-Type': 'text/html' });

And instead of res.end(data) use res.send(data) or better yet res.send({ data }).


EDIT I didn't notice that you didn't use express, try this:

res.writeHead(200, { 'Content-Type': 'application/json' });
res.write(JSON.stringify(data));
res.end();
Soandso answered 18/5, 2020 at 12:11 Comment(5)
Still fails, message: 'res.send is not a function', code: 'TypeError', time: 2020-05-18T12:59:37.706Z } Note I'm not using ExpressAthiste
It still fails --> The first argument must be of type string or an instance of Buffer. Received an instance of ObjectAthiste
Sorry, do this res.write(JSON.stringify(data));Soandso
That works Perfectly. Could you please explain why we used JSON.stringify and when should i use it and other methods available??Athiste
It's because data's data type is an object/json, and res.write() accepts data type string or buffer. JSON.stringify converts the object to a string basically.Soandso
I
14

In case this may help someone, try to use the objectMode property.

something like it

const t = new Transform({
    objectMode: true, // set this one to true
    transform(data, _, done) {
      //...your code
    }
  });
Isodiametric answered 13/3, 2022 at 18:29 Comment(0)
H
2

In case this may help someone, I was using data type other than String, Number to be exact, so I changed it to String to resolve this error

Heartfelt answered 6/7, 2021 at 6:39 Comment(0)
A
2

In case of ExpressJS, the following solution worked for me:

const response = await s3client.send(command);
// The Body object also has 'transformToByteArray' and 'transformToWebStream' methods.
const data = await response.Body.transformToByteArray()
const buffer = Buffer.from(data);       
res.attachment(`filename.pdf`).contentType("application/pdf").send(buffer)
Ancalin answered 22/4, 2023 at 14:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.