用nodejs在EJ中渲染图像数据
我有一个Nodejs Express系统,并希望渲染QR代码Datablock的图像。
我正在从我的词汇架构中生成图像
BaseUserSchema.methods.getProvisionImage = function getProvisionImage() {
qr.toDataURL( JSON.stringify({
account: this.accountName,
code: this.provisionCode,
}),
{},
function (err, url) {
if (err) throw err;
return url;
});
};
,并想在页面上绘制图像
<img class="img-thumbnail m-2 d-block"
src="<% provisionUser.getProvisionImage() %>"
title="Click to enlarge" >
正确的供应手是正确的,并且在我进行调试时,在生成代码结束时的值
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAK ... [etc] ... ==
对我来说看起来都很好 - 我该如何将该字符串转换为可以显示的东西?
I have a nodejs express system, and want to render an image of a qr code datablock.
I am generating the image from my mongoose Schema
BaseUserSchema.methods.getProvisionImage = function getProvisionImage() {
qr.toDataURL( JSON.stringify({
account: this.accountName,
code: this.provisionCode,
}),
{},
function (err, url) {
if (err) throw err;
return url;
});
};
and want to draw in an image on the page
<img class="img-thumbnail m-2 d-block"
src="<% provisionUser.getProvisionImage() %>"
title="Click to enlarge" >
The provisionUser is correct, and the value at the end of the generation code when I am debugging is
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAK ... [etc] ... ==
This all looks pretty good to me - how do I convert that string into something that an can display?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于
ejs
不支持带有回调的异步函数,您唯一的方法可能是使用异步/等待。您可以尝试以下操作:
编辑
,并解释为什么您代码无法正常工作:
Since
ejs
does not support asynchronous functions with callback your only way to get around it might be to use async/await.You can try this:
Edit
And to explain why you code can't work:
尝试对该方法进行诺言,然后等待结果将其传递给
ejs
:try promisifying the method, and then await the result into a variable which will be passed to
ejs
: