在node.js中,我有一个rest api,所有上传的图像都很大,请调整大小。
调整大小的图像的文件路径被编译并返回。下面是我的代码。我遇到错误
': await is only valid in async function'
如何调用我的await方法?我无法找到代码。
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";
};
答案 0 :(得分:0)
您需要使CB函数异步:
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]);
}
});
答案 1 :(得分:0)
您需要更改创建async
函数的位置。
app.post
的回调函数中不需要它,但是sizeOf
的回调中将需要它,因为这是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";
};
由于您将async
包裹在for...of
循环中,因此这对您来说很棘手。
您最好在该级别使用await;
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];
}
}
}
在您的函数中,
for await (image of getImage()) {
imagelist.push(image)
}
答案 2 :(得分:0)
您需要使函数(在其中使用await)成为异步函数。
关于此的更多信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
赞:
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";
};