获取TypeError:这不是在mocha中使用Buffer.from的类型化数组

时间:2022-01-01 20:20:29

I'm using Mocha / Chai to unit test a library that has recently started using nodejs' Buffer objects to solve a different problem.

我正在使用Mocha / Chai对最近开始使用nodejs的Buffer对象来解决不同问题的库进行单元测试。

I get this error message in the unit test:

我在单元测试中收到此错误消息:

TypeError: this is not a typed array. 
at Function.from (native) 
at Object.hashesMatch (index.js:29:18
at Context.<anonymous> (test/test.js:25:22)

Line 29 of index.js is where I'm using nodejs' Buffer...

index.js的第29行是我使用nodejs的Buffer ...

var b = Buffer.from ('some string or other');

I can't find a polyfill or workaround so would be grateful for suggestions.

我找不到填充物或解决方法,所以会感激建议。

Thanks

谢谢

2 个解决方案

#1


40  

You might be using an old version of Node.js.

您可能正在使用旧版本的Node.js.

Buffer.from was introduced in version 6.0.0:

Buffer.from是在6.0.0版本中引入的:

To make the creation of Buffer objects more reliable and less error prone, the various forms of the new Buffer() constructor have been deprecated and replaced by separate Buffer.from(), Buffer.alloc(), and Buffer.allocUnsafe() methods.

为了使Buffer对象的创建更可靠且更不容易出错,新的Buffer()构造函数的各种形式已被弃用,并由单独的Buffer.from(),Buffer.alloc()和Buffer.allocUnsafe()方法替换。 。

There's no reference to this method in previous versions of documentiation.

在先前版本的文档化中没有提到这种方法。

You could either update to 6.0.0 or use a deprecated constructor API, which has the following signature:

您可以更新到6.0.0或使用不推荐使用的构造函数API,它具有以下签名:

new Buffer(str[, encoding])

#2


4  

I also got the same error. You can try this

我也有同样的错误。你可以试试这个

var b = new Buffer('some string or other');

Second param is encoding (optional). By default encoding will be utf-8

第二个参数是编码(可选)。默认编码为utf-8

#1


40  

You might be using an old version of Node.js.

您可能正在使用旧版本的Node.js.

Buffer.from was introduced in version 6.0.0:

Buffer.from是在6.0.0版本中引入的:

To make the creation of Buffer objects more reliable and less error prone, the various forms of the new Buffer() constructor have been deprecated and replaced by separate Buffer.from(), Buffer.alloc(), and Buffer.allocUnsafe() methods.

为了使Buffer对象的创建更可靠且更不容易出错,新的Buffer()构造函数的各种形式已被弃用,并由单独的Buffer.from(),Buffer.alloc()和Buffer.allocUnsafe()方法替换。 。

There's no reference to this method in previous versions of documentiation.

在先前版本的文档化中没有提到这种方法。

You could either update to 6.0.0 or use a deprecated constructor API, which has the following signature:

您可以更新到6.0.0或使用不推荐使用的构造函数API,它具有以下签名:

new Buffer(str[, encoding])

#2


4  

I also got the same error. You can try this

我也有同样的错误。你可以试试这个

var b = new Buffer('some string or other');

Second param is encoding (optional). By default encoding will be utf-8

第二个参数是编码(可选)。默认编码为utf-8