【发布时间】:2020-11-23 13:28:48
【问题描述】:
我正在尝试对图像进行 OCR 解析。
所有的东西都很好,但我有一个问题,这个猫鼬和同步。
但不能在 mongoose find 调用中使用“await”,因为该函数不是异步的。我该如何解决。
这是我的代码:
// post processImage
router.post('/', async (req, res) => {
try {
var baseUrl;
const form = formidable({ multiples: true });
form.parse(req, function (error, fields, files) {
var imatgeAProcessar = files.image.path;
var extname = path.extname(files.image.name);
getTextFromImage(imatgeAProcessar) // OCR process of the image
.then(res => {
const boss_name_req = res.boss_name;
const boss = Boses.findOne({"name" : boss_name_req}).exec();
// ERROR HERE // return nothing althought it exist on database (no await?)
console.log(JSON.stringify(boss)); // writes "{}"
const processedImage = {
"success": true,
"boss_name": boss.name,
"boss_image": baseUrl + 'images/' + boss.num + ".png"
}
res.json(processedImage);
})
});
} catch (err) {
res.json({message: err});
}
});
【问题讨论】:
-
你试过数据库中的查询吗?请提供有关您导入的“Boses”的更多信息。
-
是的,对数据库的查询有效!
-
1. .find() 作为数组返回。所以'boss.name'将是无效的
-
豺狼正确。我改为 findOne .... 仍然是同样的问题。事情不是在等待结果。
标签: node.js mongoose formidable