我正在运行以下代码,但由于以下错误而失败。
AWS代码以列出存储卡中的所有对象
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}`)
第一个输出是console.log,按预期显示所有内容。 但是,将输出呈现到屏幕的res.end失败,并出现以下错误。
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
}
非常感谢您的帮助。
谢谢 阿尼什(Anish)
答案 0 :(得分:4)
删除此res.writeHead(200, { 'Content-Type': 'text/html' });
并且使用res.end(data)
或更好的res.send(data)
代替res.send({ data })
。
编辑 我没有注意到您没有使用快递,请尝试以下操作:
res.writeHead(200, { 'Content-Type': 'application/json' });
res.write(JSON.stringify(data));
res.end();
答案 1 :(得分:0)
如果这可能对某人有所帮助,准确地说,我使用的数据类型不是 String
、Number
,因此我将其更改为 String
以解决此错误