TypeError: Can not read property '1' of null

0

I have this problem and I have no idea how to solve it, 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 out here

  

let type = matches [1];

But I can not figure out how to solve it, and the error message:

  

TypeError: Can not read property '1' of null

It does not help much, can anyone help?

    
asked by anonymous 12.11.2018 / 17:54

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');
}
    
12.11.2018 / 18:02