在 Node 4.3 中使用 Buffer.from 时,TypeError Base64 不是函数
TypeError Base64 is not a function when using Buffer.from in Node 4.3
我正在使用标准 Amazon 机器映像 (linux x64) 在节点 4.3 上本地调试 lambda 函数
当我 运行 程序时,我在一个旨在将 base64 字符串解码为 utf-8 的函数中遇到错误。错误说
Unhandled rejection TypeError: base64 is not a function
at Function.from (native)
at Function.from (native)
at /home/ec2-user/sass-compiler/lib/compiler.module.js:26:30
我的代码看起来很简单。我检查了 SO 和 node 4.3 docs,我的代码似乎符合要求。知道我可能做错了什么吗?
template() {
return new Promise((resolve, reject) => {
let buf = Buffer.from(this._opts.tpl, 'base64').toString('utf-8');
let _tpl = _.template(buf);
resolve(_tpl(this._opts.opts));
});
}
编辑该程序使用 Node LTS 6.9.5
在 Windows 10 上运行良好
我终于在 Stack Overflow 上找到了回答这个问题的答案。
NodeJS: How to decode base64 encoded string back to binary?
在节点 5.3 及以下版本中,您需要使用缓冲区构造函数(即:new Buffer
)而不是 Buffer.from
我正在使用标准 Amazon 机器映像 (linux x64) 在节点 4.3 上本地调试 lambda 函数
当我 运行 程序时,我在一个旨在将 base64 字符串解码为 utf-8 的函数中遇到错误。错误说
Unhandled rejection TypeError: base64 is not a function
at Function.from (native)
at Function.from (native)
at /home/ec2-user/sass-compiler/lib/compiler.module.js:26:30
我的代码看起来很简单。我检查了 SO 和 node 4.3 docs,我的代码似乎符合要求。知道我可能做错了什么吗?
template() {
return new Promise((resolve, reject) => {
let buf = Buffer.from(this._opts.tpl, 'base64').toString('utf-8');
let _tpl = _.template(buf);
resolve(_tpl(this._opts.opts));
});
}
编辑该程序使用 Node LTS 6.9.5
在 Windows 10 上运行良好我终于在 Stack Overflow 上找到了回答这个问题的答案。
NodeJS: How to decode base64 encoded string back to binary?
在节点 5.3 及以下版本中,您需要使用缓冲区构造函数(即:new Buffer
)而不是 Buffer.from