node.js 获取 TypeError:这不是在 mocha 中使用 Buffer.from 的类型化数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36899888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting TypeError: this is not a typed array using Buffer.from in mocha
提问by VorpalSword
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.
我找不到 polyfill 或解决方法,因此非常感谢您的建议。
Thanks
谢谢
回答by Роман Парадеев
You might be using an old version of Node.js.
您可能正在使用旧版本的 Node.js。
Buffer.fromwas introducedin 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 versionsof 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])
回答by Mandeep Singh
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
回答by abhinav pandey
There are times when its difficult to update the node version especially if you are using at production so the another solution is
有时很难更新节点版本,特别是如果您在生产中使用,那么另一种解决方案是
use "kafka-node": "1.6.2"or lesser
使用"kafka-node": "1.6.2"或更小

