【发布时间】:2021-08-13 21:32:25
【问题描述】:
我创建了一个函数 -
public getImageFile(imageFileName: string) {
const directoryPath = path.join(__dirname, '../images');
fs.readdir(directoryPath, (e, images) => {
if (e) {
console.log(e)
}
const filteredImageFilesArray = images.filter(i => imageFileName.includes(i));
filteredImageFilesArray.forEach((imageFile) => {
return imageFileName = imageFile;
});
});
}
函数获取一个srting,字符串是来自DB的图像文件名,它将文件名与文件夹进行比较并返回匹配的文件。
我想将文件从服务器发送到客户端。
这是服务器响应 -
res.status(200).send({
success: true,
message: "Successfully retrieved products",
data: products.map((product) => ({
id: product.id as string,
category: {
value: product.category,
label: ServerGlobal.getInstance().getCategoryLabel(product.category)!,
},
gender: {
value: product.gender,
label: ServerGlobal.getInstance().getGenderLabel(product.gender)!,
},
title: product.title,
description: product.description,
price: product.price,
imageFileName: ServerGlobal.getInstance().getImageFile(product.imageFileName),
})),
});
我在 数据 上遇到错误 -
属性“imageFileName”的类型不兼容。 类型 'void' 不可分配给类型 'string'.ts(2322)
这是我的回复界面-
type IgetProductsResponse = express.Response<
IServerResponse & {
data?: {
id: string;
category: { value: ProductCategory, label: string };
gender: { value: ProductGender, label: string };
title: string;
description: string;
price: number;
imageFileName: File;
}[];
}
>;
我能为成功做些什么?
【问题讨论】:
标签: javascript node.js typescript