如何将数据分配为节点中的变量?

How can i assign data as variable in node?

我想要下面的功能 return 我的 img 以便我以后可以使用它。目前它仅以以下身份登录到控制台:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHQAAAB0CAYAAABUmhYnAAAAAklEQVR4AewaftIAAAK0SURBVO3BQW7kQAwEwSxC//9yro88NSBIM2sTjIg/WGMUa5RijVKsUYo1SrFGKdYoxRqlWKMUa5RijVKsUYo1SrFGKdYoxRrl4qEkfJNKl4RO5SQJJypdEr5J5YlijVKsUYo1ysXLVN6UhCeScKJyh8qbkvCmYo1SrFGKNcrFhyXhDpU7VLok3JGETuWOJNyh8knFGqVYoxRrlIs/Lgmdyh1JmKRYoxRrlGKNcvHHqdyRhE5lkmKNUqxRijXKxYep/GZJ6FTuUPlNijVKsUYp1igXL0vCNyWhU+mS0Kl0SbgjCb9ZsUYp1ijFGiX+4A9LwptU/rJijVKsUYo1ysVDSehUuiScqHRJuEPljiTckYRO5SQJnUqXhBOVJ4o1SrFGKdYoFx+mcpKETuUkCd+kcpKEO1S6JLypWKMUa5RijXLxYUnoVDqVkyR8ksodSehUTpLwTcUapVijFGuUi4dUuiR0Kl0STlQ6lS4J/5NKl4QTlZMkvKlYoxRrlGKNEn/wRUnoVLoknKjckYROpUtCp9IloVP5zYo1SrFGKdYo8Qd/WBKeULkjCScqXRJOVN5UrFGKNUqxRrl4KAnfpHKicpKELgmdyhNJ6FS+qVijFGuUYo1y8TKVNyXhiSR0KidJ6FQ6lS4JJ0m4Q+WJYo1SrFGKNcrFhyXhDpU7VLokdConSehU7lDpknCi8knFGqVYoxRrlIs/LgknSehUOpWTJJyonKh0SehU3lSsUYo1SrFGuRhGpUvCSRI6lROVO5LQqXxSsUYp1ijFGuXiw1Q+SeVE5Y4kvEnlJAmdyhPFGqVYoxRrlIuXJeGbknCi0iWhU+mScEcSOpUuCZ1Kp/KmYo1SrFGKNUr8wRqjWKMUa5RijVKsUYo1SrFGKdYoxRqlWKMUa5RijVKsUYo1SrFGKdYo/wCSxQr0ueVFqAAAAABJRU5ErkJggg==

const QRCode = require('qrcode')

function getData(){
    QRCode.toDataURL('some string', function (err, img) {
        console.log(img)
    })
}

getData()

您需要在 qrcode.toDataUrl() 完成后将数据分配给变量。您可以在当前拥有的回调中执行此操作,或者您可以使用 .then(x => { }); 因为 toDataUrl() returns 承诺的重叠之一。

const QRCode = require('qrcode')

var data;

QRCode.toDataURL('some string').then(qr => {
    data = qr; // After toDataUrl is finished, the qr data is assigned to the data variable
});