【发布时间】:2025-12-11 12:35:01
【问题描述】:
检索3条记录并放入dbResult时如何使用promise join? 目前,我有一条记录检索如下代码,
req.oracleMobile.storage.getById(registry.getIncidentPhotoStorageName(), incident_id + '_01', { sync: true }).then(
function (result) {
base64data = result.result;
base64data = JSON.parse(base64data);
dbResult.photo = base64data.image;
res.status(result.statusCode).send(dbResult);
},
function (err) {
dbResult.photo = imageData.uploadPhotoIcon;
//ignore no photo and send db result
res.status(200).send(dbResult);
}
);
我想检索 3 条记录并将其添加为 dbResult.photo2 = base64data.image;使用承诺加入。 我试过“return promise.join(retrieve1,retrieve2,retrieve3);”。它不起作用。 这是我的代码...
function getIncident(req, res) {
getIncidentRow(req).then(
function (dbResult) {
var incident_id = dbResult.id;
const join = require('promise-join');
return join(req.oracleMobile.storage.getById(registry.getIncidentPhotoStorageName(), incident_id + '_01', { sync: true }).then(
function (result) {
base64data = result.result;
base64data = JSON.parse(base64data);
dbResult.photo = base64data.image;
//res.status(result.statusCode) .send(dbResult);
},
function (err) {
//ignore no photo and send db result
res.status(200).send(dbResult);
}
) ,req.oracleMobile.storage.getById(registry.getIncidentPhotoStorageName(), incident_id + '_02', { sync: true }).then(
function (result) {
base64data = result.result;
base64data = JSON.parse(base64data);
dbResult.photo2 = base64data.image;
// res.status(result.statusCode).send(dbResult);
},
function (err) {
//ignore no photo and send db result
res.status(200).send(dbResult);
}
) ,req.oracleMobile.storage.getById(registry.getIncidentPhotoStorageName(), incident_id + '_03', { sync: true }).then(
function (result) {
base64data = result.result;
base64data = JSON.parse(base64data);
dbResult.photo3 = base64data.image;
res.status(result.statusCode).send(dbResult);
},
function (err) {
//ignore no photo and send db result
res.status(200).send(dbResult);
}
), function (result) {res.status(result.statusCode).send(dbResult)}
);
}
);
}
【问题讨论】:
-
"promise.join" - 如果这是蓝鸟承诺的事情,也许添加相关标签以便蓝鸟人可以提供帮助:p
-
@JaromandaX ,当我使用 return promise.join (retrieve1,retrieve2,retrieve3); 时它不起作用可能,我的代码结构是错误的。 :(
-
好吧,Promise.join 记录在 here 并且要求最后一个参数是 handler function - 不知道为什么 Promise 库会诉诸回调,但是,我还没有阅读 Promise.join 的用例
-
@JaromandaX 我得到了数据。但是,照片出来 1 或 2,有时,它会完全出来 3 张照片。看来诺言迫不及待地加载了。仍在努力解决这个问题。 :(
-
你能把代码贴在你使用 .join ... 的地方吗,并显示retrieve1 2 和 3 实际上是什么。将代码放在问题中而不是评论中
标签: javascript node.js npm promise bluebird