resize-img 模块预期 `buffer` 的类型为 `Buffer`,但接收的类型为 `object`记录参数返回缓冲区

问题描述

未处理的拒绝(类型错误):预期 buffer 的类型为 Buffer,但收到的类型为 object push../node_modules/resize-img/index.js.module.exports

let pathi = fs.readFileSync(path.join(this.props.dir.server,"server-icon-0.png"));
console.log(pathi);

const image = await resizeImg(pathi,{
     width: 64,height: 64,});

fs.writeFileSync(path.join(this.props.dir.server,"server-icon.png"),image);

记录 pathi 返回 Buffer

Buffer(77763)

它是一个 Uint8Array 的实例,其中显然有 77k+ 个数字。

在不同的项目中运行此代码有效。我的代码或我的项目有问题吗?

解决方法

Anatoly 的帮助下找到了答案。

检查 pathi 是否真的是 Buffer 的实例,Buffer.isBuffer(pathi) 返回 false。使用 Buffer(pathi) Buffer.isBuffer(Buffer(pathi)) 构造新缓冲区后返回 true!