获取TypeError:这不是在mocha中使用Buffer.from的types数组
我正在使用Mocha / Chai来unit testing一个最近开始使用nodejs的Buffer对象来解决不同的问题的库。
我在unit testing中得到这个错误信息:
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)
index.js第29行是我使用nodejs的缓冲区…
var b = Buffer.from ('some string or other');
我无法find一个polyfill或解决方法,所以将不胜感激的build议。
谢谢
您可能正在使用旧版本的Node.js。
Buffer.from
是在6.0.0版本中引入的 :
为了使Buffer对象的创build更可靠,更less出错,新的Buffer()构造函数的各种forms已经被弃用,并被单独的Buffer.from(),Buffer.alloc()和Buffer.allocUnsafe()方法取代。
在以前版本的documentiation中没有提到这种方法。
您可以更新到6.0.0或使用不赞成的构造函数API,它具有以下签名:
new Buffer(str[, encoding])
我也有同样的错误。 你可以试试这个
var b = new Buffer('some string or other');
第二个参数是编码(可选)。 默认编码是utf-8