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

Vor*_*ord 22 mocha.js node.js

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

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

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)
Run Code Online (Sandbox Code Playgroud)

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

var b = Buffer.from ('some string or other');
Run Code Online (Sandbox Code Playgroud)

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

谢谢

Ром*_*еев 43

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

Buffer.from 在6.0.0版中引入:

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

先前版本的文档中没有提及此方法.

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

new Buffer(str[, encoding])
Run Code Online (Sandbox Code Playgroud)

  • 顺便说一句,它被反向移植到v4.5.0 https://nodejs.org/docs/v4.5.0/api/buffer.html#buffer_class_method_buffer_from_array和v5.10.0 - https://nodejs.org/docs/v5.10.0/api /buffer.html#buffer_class_method_buffer_from_array (5认同)

Man*_*ngh 5

我也得到了同样的错误.你可以试试这个

var b = new Buffer('some string or other');
Run Code Online (Sandbox Code Playgroud)

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