简体   繁体   中英

': await is only valid in async function'

In node.js, i have a rest api, all images uploaded that big in size, get resized.

file path of resized image is compiled and returned. below is my code. i get error

': await is only valid in async function'

How do i call my await method? i am unable to figure the code.

app.post('/upload', upload.array('file'), async (req, res) => {
    var imagelist = [];
    for (const file of req.files) {         
        var sizeOf = require('image-size');
        sizeOf(__basefolder + file.originalname, function (err, dimensions) {
            //image quite big. resize it.
            if (dimensions.width > 128 && dimensions.height > 128) {
                console.log('more than 128');
               // ############### ERROR LINE BELOW ##################################
                const resize_2 = await resizeImage(__basefolder, file.originalname, dimensions, 2);
                imagelist.push([resize_2, file.originalname]);
            }
        });      
    };
    res.json(imagelist);
});

async function resizeImage(folder, fileName, dimensions, divisor) {
    return "resized image file path";
};

you need the make the CB function async like:

sizeOf(__basefolder + file.originalname, async (err, dimensions) => {
            //image quite big. resize it.
            if (dimensions.width > 128 && dimensions.height > 128) {
                console.log('more than 128');
               // ############### ERROR LINE BELOW ##################################
                const resize_2 = await resizeImage(__basefolder, file.originalname, dimensions, 2);
                imagelist.push([resize_2, file.originalname]);
            }
        });   

You need to change where you create the async function.

It's not needed in the callback function for app.post , but you would need it in the callback for sizeOf since that's the wrapping function for your await

app.post('/upload', upload.array('file'), (req, res) => {
    var imagelist = [];
    for (const file of req.files) {         
        var sizeOf = require('image-size');
        sizeOf(__basefolder + file.originalname, async function (err, dimensions) {
            //image quite big. resize it.
            if (dimensions.width > 128 && dimensions.height > 128) {
                console.log('more than 128');
               // ############### ERROR LINE BELOW ##################################
                const resize_2 = await resizeImage(__basefolder, file.originalname, dimensions, 2);
                imagelist.push([resize_2, file.originalname]);
            }
        });      
    };
    res.json(imagelist);
});

async function resizeImage(folder, fileName, dimensions, divisor) {
    return "resized image file path";
};

This is going to get tricky for you since you're wrapping your async within a for...of loop.

You're better off using await at that level;

async function* getImage(files) {
  for (file of files) {
    const {
      err,
      dimensions
    } = await sizeOf(__basefolder + file.originalname);
    if (dimensions.width > 128 && dimensions.height > 128) {
      console.log('more than 128');
      // ############### ERROR LINE BELOW ##################################
      const resize_2 = await resizeImage(__basefolder, file.originalname, dimensions, 2);
      yield [resize_2, file.originalname];
    }
  }
}

And in your function,

for await (image of getImage()) {
  imagelist.push(image)
}

You need to make your function (which uses await inside it) an async function.

Here is more on this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

Like:

app.post('/upload', upload.array('file'), async (req, res) => {
var imagelist = [];
for (const file of req.files) {         
    var sizeOf = require('image-size');
    sizeOf(__basefolder + file.originalname, async function (err, dimensions) {
        //image quite big. resize it.
        if (dimensions.width > 128 && dimensions.height > 128) {
            console.log('more than 128');
           // ############### ERROR LINE BELOW ##################################
            const resize_2 = await resizeImage(__basefolder, file.originalname, dimensions, 2);
            imagelist.push([resize_2, file.originalname]);
        }
    });      
};
res.json(imagelist);
});

async function resizeImage(folder, fileName, dimensions, divisor) {
  return "resized image file path";
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM