Typeerror: Cannot read Property '1' of null

Asked

Viewed 1,399 times

0

I have this problem and I have no idea how to solve, I want to save images in Azure Storage

const blobSvc = azure.createBlobService(config.containerConnectionString);

    let filename = guid.raw().toString() + '.jpg';
    let rawdata = req.body.image;
    let matches = rawdata.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/);
    let type = matches[1];
    let buffer = new Buffer(matches[2], 'base64');

await blobSvc.createBlockBlobFromText('images', filename, buffer, {
            contentType: type
        }, function (error, result, response) {
            if (error) {
                filename = 'default-product.png'
            }
        });

The error is pointed here

Let type = Matches[1];

but I can’t understand to solve, and the error message:

Typeerror: Cannot read Property '1' of null

it doesn’t help much, someone can help?

  • 1

    That mistake is probably because the match is null. IE, found nothing with the regex used.

1 answer

0


Most likely your rawdata.match is null. To avoid error check your rawdata variable before:

if (matches)
{
    let type = matches[1];
    let buffer = new Buffer(matches[2], 'base64');
}
  • If the rawdata were null the mistake would be: TypeError: rawdata is null. You would have to test matches and not rawdata.

  • @fernandosavio, exact.. I wrote one thing and put it in if other.. Adjusted, grateful!

  • Show! o -1 was mine to avoid giving wrong information.

  • @8bit, thanks worked, just had to trade Let for var

Browser other questions tagged

You are not signed in. Login or sign up in order to post.