node.js使用报错“The chunk argument must be of type string or an instance of Buffer or Uint8Array“

42 篇文章 0 订阅

异常

node:internal/errors:464
    ErrorCaptureStackTrace(err);
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of Array
    at new NodeError (node:internal/errors:371:5)
    at write_ (node:_http_outgoing:742:11)
    at ServerResponse.end (node:_http_outgoing:855:5)
    at F:Pre_CodingPre_WebStorm_Projectsimple-bbs-server-nodejs
outer.js:41:14
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  code: 'ERR_INVALID_ARG_TYPE'
}

Node.js v17.1.0

错误代码

使用express+node.js写web端代码:

app.get('/test', function (request, response) {
    var userList = userModel.selectAll();
    response.end(userList);
});

原因

response.end()方法内传入的参数应该是一个字符串,而不是一个对象,这里userModel.selectAll()的查询结果是一个对象。

解决

利用JSON.stringify()方法将对象转换成json格式的字符串返回。

或者使用node.js的http模块的send()方法,可以直接传递对象。

正确代码

  • 使用JSON.stringify()方法:

    app.get(’/test’, function (request, response) {
    var userList = userModel.selectAll();
    response.end(JSON.stringify(userList));
    });

  • 使用send()方法

    app.get(’/test’, function (request, response) {
    var userList = userModel.selectAll();
    response.send(userList);
    });

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"The 'buffer' argument must be one of type Buffer, Uint8Array, or string. Received type object." 这个错误是由于给定的buffer参数类型错误导致的。根据引用,错误信息表明"chunk"参数应该是一个字符串或者Buffer实例或者Uint8Array实例,而不是一个对象实例。根据引用,Uint8Array是一个内置变量,不需要额外引入包来使用。通常,解决这类问题的通用方法是确保参数是Buffer或Uint8Array的实例。另外,引用指出,如果要将多个Buffer连接在一起,需要确保这些Buffer都是Uint8Array类型的。正确的做法是将buffer参数改为正确的类型,比如Buffer、Uint8Array或字符串。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [node.js报错The “chunkargument must be of type string or an instance of Buffer or Uint8Array.](https://blog.csdn.net/weixin_42771853/article/details/130218226)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Nodejs: Buffer报错argument must be an Array of Buffer or Uint8Array instances](https://blog.csdn.net/pengpengzhou/article/details/106474846)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值