用nodejs在EJ中渲染图像数据

发布于 2025-01-27 06:35:30 字数 729 浏览 2 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我们只是彼此的过ke 2025-02-03 06:35:30

由于ejs不支持带有回调的异步函数,您唯一的方法可能是使用异步/等待。

您可以尝试以下操作:

BaseUserSchema.methods.getProvisionImage = async function getProvisionImage() {
    var data = '';
    try {
        data = await qr.toDataURL(JSON.stringify({
            account: this.accountName,
            code: this.provisionCode,
        }));
    } catch (e) {}
    return data;
};

编辑

// this function no longer needs to be async since i moved the async code to another async function
BaseUserSchema.methods.getProvisionImage = function getProvisionImage() {
    var data = getData(JSON.stringify({
        account: this.accountName,
        code: this.provisionCode,
    }));

    return data;
};

async function getDataURL(str) {
    var data = '';
    try {
        data = await qr.toDataURL(str);
    } catch (e) {}
    return data;
};

,并解释为什么您代码无法正常工作:

// the getProvisionImage function does not return anything
// since you do not have a return statement in the getProvisionImage scope
BaseUserSchema.methods.getProvisionImage = function getProvisionImage() {
    qr.toDataURL( JSON.stringify({
        account: this.accountName,
        code: this.provisionCode,
    }),
    {},
    function callback(err, url) { // this function is the scope of the return statement bellow

        if (err) throw err;
        // this will return a value to the caller of this function
        // not the caller of getProvisionImage
        return url;
    });
    
    // this return will work, not the above one in the callback function
    return 'data:image/png;bas....';
};

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:

BaseUserSchema.methods.getProvisionImage = async function getProvisionImage() {
    var data = '';
    try {
        data = await qr.toDataURL(JSON.stringify({
            account: this.accountName,
            code: this.provisionCode,
        }));
    } catch (e) {}
    return data;
};

Edit

// this function no longer needs to be async since i moved the async code to another async function
BaseUserSchema.methods.getProvisionImage = function getProvisionImage() {
    var data = getData(JSON.stringify({
        account: this.accountName,
        code: this.provisionCode,
    }));

    return data;
};

async function getDataURL(str) {
    var data = '';
    try {
        data = await qr.toDataURL(str);
    } catch (e) {}
    return data;
};

And to explain why you code can't work:

// the getProvisionImage function does not return anything
// since you do not have a return statement in the getProvisionImage scope
BaseUserSchema.methods.getProvisionImage = function getProvisionImage() {
    qr.toDataURL( JSON.stringify({
        account: this.accountName,
        code: this.provisionCode,
    }),
    {},
    function callback(err, url) { // this function is the scope of the return statement bellow

        if (err) throw err;
        // this will return a value to the caller of this function
        // not the caller of getProvisionImage
        return url;
    });
    
    // this return will work, not the above one in the callback function
    return 'data:image/png;bas....';
};
樱桃奶球 2025-02-03 06:35:30

尝试对该方法进行诺言,然后等待结果将其传递给ejs

BaseUserSchema.methods.getProvisionImage = function getProvisionImage() {

    return new Promise((resolve, reject) => {
    
        qr.toDataURL(JSON.stringify({
                account: this.accountName,
                code: this.provisionCode,
            }), {},
            function(err, url) {

                if (err) reject(err);

                resolve(url);
            });
    });
};

//...

const img = await provisionUser.getProvisionImage();

//...
src="<% img %>"

try promisifying the method, and then await the result into a variable which will be passed to ejs:

BaseUserSchema.methods.getProvisionImage = function getProvisionImage() {

    return new Promise((resolve, reject) => {
    
        qr.toDataURL(JSON.stringify({
                account: this.accountName,
                code: this.provisionCode,
            }), {},
            function(err, url) {

                if (err) reject(err);

                resolve(url);
            });
    });
};

//...

const img = await provisionUser.getProvisionImage();

//...
src="<% img %>"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文