【发布时间】:2021-03-16 11:41:41
【问题描述】:
我在 3 天前尝试从后端读取图像,这是我尝试过的最佳解决方案,但出现错误并且不理解错误
请帮忙
get_image:(req,res)=>{
const directoryPath = __basedir + "/uploads/";
const baseUrl = "http://localhost:8080/test/get_image/";
fs.readdir(directoryPath, function (err, files) {
if (err) {
res.status(500).send({
message: "Unable to scan files!",
});
}
let fileInfos = [];
files.forEach(Image.findAll().then(data=>{ <<=== error here
fileInfos.push({
name: data.name,
url: baseUrl+data.name,
});
}))
})
}
错误:
files.forEach(Image.findAll().then(data=>{
^
TypeError: #<Promise> is not a function
【问题讨论】:
-
.forEach( foo )期望foo是一个将为每个元素执行的函数。但是,在您的情况下,foo = Image.findAll().then( /* ... */ )这是一个承诺,而不是一个功能。 -
您已将
Promise传递给files.forEach(...)而不是回调函数。 -
同样的问题:files.forEach(foo) =>TypeError: #
is not a function -
这正是我试图解释的......你不能将承诺传递给
forEach。而且我不知道你想做什么,所以我不能告诉你正确的代码是什么。