I have a function that works, the problem is that I want it to work again every time there is a change in the variable "QR" (variable QR receives updates from the cloud) and it stays the same. The function that should be enabled "QRCode.toDataURL".
client.on('qr', qr => {
qrcode.generate(qr, {small: true});
let qrcode1 = qr;
console.log(qr)
app.get('/', function (req, res) {
let opts = {
errorCorrectionLevel: 'H',
type: 'image/jpeg',
quality: 0.3,
margin: 1,
color: {
dark:"#010599FF",
light:"#40FF60"
}
}
QRCode.toDataURL(qr, opts, function (err, url) {
if (err) throw err
res.send(`<img src='${src = url}'>`)
})
})
});
CodePudding user response:
You need to save it in a global variable, it should be an array or object since we need reference.
const qrCodes = [] //
update that entry of index 0 every time you get new qr code.
client.on('qr', (qr) => {
qrCodes[0] = qr
})
consume the same every time request comes in
app.get('/', function (req, res) {
let opts = {
errorCorrectionLevel: 'H',
type: 'image/jpeg',
quality: 0.3,
margin: 1,
color: {
dark: '#010599FF',
light: '#40FF60',
},
}
const qrCode = qrCodes[0]
if (!qrCode) return res.send('')
QRCode.toDataURL(qr, opts, function (err, url) {
if (err) throw err
res.send(`<img src='${(src = url)}'>`)
})
})
It might be better to convert qrcode into DataURL string on the fly and send that string directly to user instead of generating it over 'n over but I leave it to you.